简体   繁体   中英

Subtitle for Subplots within for-loop

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.

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM