3

I have the following code to create a 9x3 plot of 27 subplots:

fig, axes = plt.subplots(9,3,figsize=(30,80))
for ax, df in zip(axes.flat, [district_1_change, district_2_change, district_3_change, 
                              district_4_change, district_5_change, district_6_change, 
                              district_7_change, district_8_change, district_9_change, 
                              district_10_change, district_11_change, district_12_change, 
                              district_13_change, district_14_change, district_15_change, 
                              district_16_change, district_17_change, district_18_change, 
                              district_19_change, district_20_change, district_21_change, 
                              district_22_change, district_23_change, district_24_change, 
                              district_25_change, district_26_change, district_27_change
                             ]):
    df[['DEM', 'REP', 'CON', 'WOR', 
                   'IND', 'GRE', 'WEP', 'REF', 'OTH', 'BLANK']].plot.bar(ax=ax,
                                                           legend=False,
#                                                            figsize=(8, 6),
                               color=['xkcd:blue',
                                                                          'xkcd:red',
                                                                          'xkcd:dark red',
                                                                          'xkcd:light blue',
                                                                          'xkcd:purple',
                                                                          'xkcd:green',
                                                                          'xkcd:pink',
                                                                          'xkcd:lilac',
                                                                          'xkcd:orange',
                                                                          'xkcd:brown'])
plt.tight_layout();

I want to add titles to each subplot. I have consulted this previous SO question but when I try to do something like:

ax = plt.subplot("931")
ax.set_title("District 1")

the data in that subplot is then erased, however, the title shows up. I would like the data and the title to both show.

thedatasleuth
  • 539
  • 4
  • 22

1 Answers1

1

I also did you the liberty of tidying up your code a little as you're fairly new to the site.

In the future try pull as much code out of your loops as possible

import math
colors = ['xkcd:blue', 'xkcd:red', 'xkcd:dark red', 'xkcd:light blue',
          'xkcd:purple', 'xkcd:green', 'xkcd:pink', 'xkcd:lilac',
          'xkcd:orange', 'xkcd:brown']
districts = [district_1_change, district_2_change, district_3_change]
cols = ['DEM', 'REP', 'CON', 'WOR', 'IND', 'GRE', 'WEP', 'REF', 'OTH', 'BLANK']

plt_cols = 3
plt_rows = int(math.ceil(len(districts)/float(plt_cols)))  # round up
fig, axes = plt.subplots(plt_rows, plt_cols, figsize=(30,80))

for i, (ax, df) in enumerate(zip(axes.flat, districts)):
    df[cols].plot.bar(ax=ax, legend=False, figsize=(8, 6), color=colors)
    ax.set_title('district_{}_change'.format(i))

plt.tight_layout()
fig.subplots_adjust(top=0.88)
fig.suptitle('My Districts')
Alexander McFarlane
  • 10,643
  • 9
  • 59
  • 100
  • This code worked although it added three extra blank subplots at the end and I now have 26 instead of 27 Districts because of 0 indexing. This will be confusing to the reader. I also wanted to be able to individually set the titles so I could describe what each District is (eg. "District 7 - Brooklyn") – thedatasleuth Sep 12 '18 at 13:48
  • The idea of the site is not for me to write your code for you. It is for me to provide an answer for your problem that you can adapt. Above is how you *"set subtitles for plots within a for loop"* – Alexander McFarlane Sep 12 '18 at 13:50