【发布时间】:2020-05-06 17:47:49
【问题描述】:
我有一个四列的 Pandas 数据框;第一个是中心类型(x 轴),我希望其余列按年份显示为每个中心类型的并排列。我不确定我做错了什么,因为我只获得了 2019 年的值,但我也希望显示 2018 年和 2017 年。
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
【问题讨论】:
-
如果我像
df = pd.DataFrame(np.random.rand(5,3), columns=[2018, 2019, 2020], index=list("ABCDE"))一样定义df并运行您的代码,我会得到所需的输出,即this。这意味着您要么没有使用您认为自己使用的数据,要么您使用的是带有错误的特定 pandas 版本。
标签: python pandas matplotlib charts seaborn