使用plt.subplots绘制多图
程序员文章站
2022-05-27 16:14:17
...
NeiBor_stat_tab = all_data.groupby(by=['Neighborhood']).agg({'SalePrice':['median','count'],'Neighborhood':['count']})
from matplotlib import pyplot as plt
fig, ax = plt.subplots(nrows=2, ncols=1, figsize=(12,6)) #ax有两个对象
ax[0].set_xticklabels(labels=NeiBor_stat_tab.index, rotation="45")
ax[0].bar(x=NeiBor_stat_tab.index, height=NeiBor_stat_tab[('SalePrice','median')],color='blue')
ax[0].set_xlabel(xlabel='Neighborhood')
ax[0].set_ylabel(ylabel='Median SalePrice')
ax[0].hlines(y=all_data['SalePrice'].median(),xmin=NeiBor_stat_tab.index[0],xmax=NeiBor_stat_tab.index[-1],
colors = "r", linestyles = "dashed")
for i,c in enumerate(NeiBor_stat_tab[('SalePrice','count')]):
ax[0].text(x=i, y=-0.5, s=c,bbox=dict(facecolor='white', alpha=0.9), horizontalalignment='center')
# alpha是bbox的透明度
ax[1].set_xticklabels(labels=NeiBor_stat_tab.index, rotation="45")
ax[1].bar(x=NeiBor_stat_tab.index, height=NeiBor_stat_tab[('Neighborhood','count')],color='blue')
ax[1].set_xlabel(xlabel='Neighborhood')
ax[1].set_ylabel(ylabel='count')
for i,c in enumerate(NeiBor_stat_tab[('Neighborhood','count')]):
ax[1].text(x=i, y=c, s=c,bbox=dict(facecolor='white', alpha=0.9), horizontalalignment='center')
plt.tight_layout(True)
上一篇: [Linux] deepin与nginx