这个问题在这里已有答案:
我在9x3单图中有27个子图的以下代码。我想为每个子图添加特定的单独标题,例如。 “7区 - 布鲁克林区。”但是,此代码重复所有27个子图的数据和标题,可能是因为最后一行代码。我已经咨询了this previous SO question并尝试了所有这三种方法,但是当添加标题或广播不正确时,每个方法都会删除子图中的数据。
fig, axes = plt.subplots(9,3,figsize=(30,80))
for ax in axes.flatten():
ax1 = district_1_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax)
ax1.set_title("District 1: Eastern Long Island", fontsize=18)
ax2 = district_2_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax)
ax2.set_title("District 2: Southern Long Island", fontsize=18)
ax3 = district_3_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax)
ax3.set_title("District 3: Northern Long Island", fontsize=18)
ax4 = district_4_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax)
ax4.set_title("District 4: Central, Southern Nassau County", fontsize=18)
ax5 = district_5_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax)
ax5.set_title("District 5: Queens", fontsize=18)
ax6 = district_6_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax)
ax6.set_title("District 6: Queens", fontsize=18)
ax7 = district_7_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax)
ax7.set_title("District 7: Queens", fontsize=18)
ax8 = district_8_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax)
ax8.set_title("District 8: Brooklyn, Queens", fontsize=18)
ax9 = district_9_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax)
ax9.set_title("District 9: Brooklyn", fontsize=18)
ax10 = district_10_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax)
ax10.set_title("District 10: Upper West Side, FiDi, Greenwich Village, Borough Park (BK)", fontsize=18)
ax11 = district_11_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax)
ax11.set_title("District 11: Staten Island, Southern Brooklyn", fontsize=18)
ax12 = district_12_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax)
ax12.set_title("District 12: Eastern Manhattan, Greenpoint (BK), Western Queens", fontsize=18)
ax13 = district_13_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax)
ax13.set_title("District 13: Upper Manhattan, Bronx", fontsize=18)
ax14 = district_14_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax)
ax14.set_title("District 14: Eastern Bronx, North Central Queens", fontsize=18)
ax15 = district_15_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax)
ax15.set_title("District 15: Southern, Western Bronx", fontsize=18)
ax16 = district_16_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax)
ax16.set_title("District 16: North Bronx, Southern Westchester ", fontsize=18)
ax17 = district_17_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax)
ax17.set_title("District 17: Rockland, Westchester", fontsize=18)
ax18 = district_18_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax)
ax18.set_title("District 18: Orange, Putnam, Southern Dutchess, Northeastern Westchester", fontsize=18)
ax19 = district_19_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax)
ax19.set_title("District 19: Hudson Valley, Catskills", fontsize=18)
ax20 = district_20_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax)
ax20.set_title("District 20: Albany, Schenectady", fontsize=18)
ax21 = district_21_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax)
ax21.set_title("District 21: Clinton, Essex, Franklin, Fulton, Hamilton, Herkimer, Jefferson, Lewis, Saratoga, St. Lawrence, Warren, and Washington", fontsize=18)
ax22 = district_22_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax)
ax22.set_title("District 22: Central New York", fontsize=18)
ax23 = district_23_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax)
ax23.set_title("District 23: Western New York", fontsize=18)
ax24 = district_24_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax)
ax24.set_title("District 24: Cayuga, Onondaga, Wayne, Western Oswego", fontsize=18)
ax25 = district_25_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax)
ax25.set_title("District 25: Monroe", fontsize=18)
ax26 = district_26_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax)
ax26.set_title("District 26: Western New York: Erie, Niagara", fontsize=18)
ax27 = district_27_change.drop(['TOTAL'], axis=1).plot.bar(legend=True, ax=ax)
ax27.set_title("District 27: Western New York", fontsize=18)
# plt.tight_layout()
这是一个使用示例DataFrame的最小工作解决方案(不包括导入),使用较小的2x3网格。您只需将其扩展到实际网格和实际数据即可。用于设置标题的重要行由注释突出显示
fig, axes = plt.subplots(2,3,figsize=(16,8))
df = pd.DataFrame({'count': {0: 37, 1: 23, 2: 10, 3: 16, 4: 45}}).reset_index()
for ax in axes.flatten():
axi = df.plot.bar(x='index', y='count', legend=False, ax=ax) # Notice ax=ax and axi here
axi.set_title("Title here", fontsize=18) # Set the title here
plt.tight_layout()
产量