for循环中子图的副标题

问题描述 投票:1回答:1

我有以下代码来创建27个子图的9x3图:

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();

我想为每个子图添加标题。我咨询了这个previous SO question,但当我尝试做类似的事情时:

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

然后删除该子图中的数据,但标题显示出来。我希望数据和标题都显示出来。

python pandas plot
1个回答
1
投票

我也让你自由整理你的代码,因为你是一个相当新的网站。

将来尝试尽可能多地从循环中提取代码

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')
© www.soinside.com 2019 - 2024. All rights reserved.