Matplotliib:从Pandas数据框中为多列创建多条形图

问题描述 投票:0回答:1

我有一个包含四列的Pandas数据框;第一个是中心类型(x轴),我希望其余各列按年份显示为每种中心类型的并排列。我不确定自己在做什么错,因为我只获得了2019年的值,但我也想显示2018年和2017年。

enter image description here

enter image description here

df = c2

colors = ['steelblue','forestgreen','salmon']

ax = df.plot(kind='bar', color=colors, width=0.7, align='center', stacked=False, rot=60, figsize=(12,6), legend=False, zorder=3)


from matplotlib.ticker import FuncFormatter, MaxNLocator
ax.yaxis.set_major_locator(MaxNLocator(integer=True))

plt.grid(zorder=0)
#plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.2))
plt.xlabel("Type of centre")
plt.ylabel("Number")
plt.title("")

ax.spines['bottom'].set_color('gainsboro')
ax.spines['top'].set_color('white') 
ax.spines['right'].set_color('white')
ax.spines['left'].set_color('gainsboro')

ax.yaxis.label.set_color('black')
ax.xaxis.label.set_color('black')

ax.title.set_color('black')

ax.patch.set_facecolor('white')

plt.savefig('images/figure1.png', dpi=300, facecolor=ax.get_facecolor(), transparent=True,  bbox_inches='tight', pad_inches=0.1)
plt.show()
data.shape
python pandas matplotlib charts seaborn
1个回答
0
投票

您的图片仅包含一列的原因是df = c2。显然,c2仅包含DataFrame中的一列。删除该指令。

作为测试,我只是执行:

df.plot.bar(width=0.7, rot=60, figsize=(12,6), legend=False);

获得以下正确的图片,每组有5条,每条3条:

enter image description here

仅从上述说明开始,然后确定剩下的指令应实际执行。

我认为,不需要省略的参数,因为它们传递各自的默认值。

还要注意最后的分号。在Jupyter Notebook下工作仅需要渲染图片。否则(没有终止分号),<...>打印在图片上。

© www.soinside.com 2019 - 2024. All rights reserved.