在 matplotlib 的 for-loop 中向子图添加唯一标题

Adding unique titles to subplots within a for-loop in matplotlib

我已经检查了 SO 上的其他答案,但似乎没有帮助我解决这个问题。我已经从 pandas df 中的列创建了子图。我希望每个 subplot 在每个标题的末尾都有一个唯一的日期,我曾试图在循环中这样做。发生的情况是标题只出现在最后一张图表上,而没有出现在其他图表上。这是为什么?

area2011 = df_2011.groupby(['Area Id']).size()
area2012 = df_2012.groupby(['Area Id']).size()
area2013 = df_2013.groupby(['Area Id']).size()
area2014 = df_2014.groupby(['Area Id']).size()
area2015 = df_2015.groupby(['Area Id']).size()
area2016 = df_2016.groupby(['Area Id']).size()
area_list = [area2011, area2012, area2013, area2014, area2015, area2016]
fig, axes = plt.subplots(2, 3)
for i in range(2011, 2017):
    for d, ax in zip(area_list, axes.ravel()):
        plt.title(f'Area Id crimes in {i}')
        d.plot.bar(ax=ax, figsize=(15, 7))
        plt.tight_layout()

从第一到最后subplot我要制作:

"Area Id crimes in 2011"
   .
   .
   .
"Area Id crimes in 2016"

我认为 post 回答更快。所以这里是:

for i in range(2011, 2017):
    for d, ax in zip(area_list, axes.ravel()):
        plt.title(f'Area Id crimes in {i}')
        d.plot.bar(ax=ax, figsize=(15, 7))
        plt.tight_layout()

外层 for 循环,最后 运行 (i=2016),会将所有内容设置为 f'Area Id crimes in {i}',即 'Area Id crimes in 2016'

for d, i in zip(area_list, range(6)):
    ax = axes.ravel()[i];
    ax.set_title(f'Area Id crimes in {i+2011}')
    d.plot.bar(ax=ax, figsize=(15, 7))
    plt.tight_layout()