【问题标题】:Subtitle for Subplots within for-loopfor循环中子图的字幕
【发布时间】: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 方法应该有效。

标签: python pandas plot


【解决方案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')

【讨论】:

  • 此代码有效,尽管它在末尾添加了三个额外的空白子图,并且由于 0 索引,我现在有 26 个而不是 27 个区域。这会让读者感到困惑。我还希望能够单独设置标题,这样我就可以描述每个区是什么(例如“第 7 区 - 布鲁克林”)
  • 网站的想法不是让我为你写你的代码。我要为你的问题提供一个你可以适应的答案。以上是您如何“在 for 循环中为绘图设置字幕”
猜你喜欢
  • 2019-09-04
  • 2023-03-19
  • 2018-07-25
  • 2022-11-17
  • 2017-08-21
  • 2019-05-13
  • 1970-01-01
  • 2020-07-23
  • 2015-07-24
相关资源
最近更新 更多