【发布时间】:2018-09-12 13:22:07
【问题描述】:
我有以下代码来创建 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")
然后删除该子图中的数据,但是,显示标题。我希望同时显示数据和标题。
【问题讨论】:
-
看我的回答:如果你在他们上面有一个主标题,这也会有所帮助stackoverflow.com/a/35676071/4013571
-
我不确定它是否重复...^
-
我查看了您的答案,但不确定如何将其合并到我的代码中,因为我使用 for 循环一次绘制所有内容,而您的代码单独绘制
-
你能创建一小组数据和图表吗?看起来 McFarlane 方法应该有效。