for循环内字幕的字幕

时间:2018-09-12 13:22:08

标签: python pandas plot

我具有以下代码来创建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")

该子图中的数据随后被删除,但是标题出现了。我想同时显示数据和标题。

1 个答案:

答案 0 :(得分: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')