在pandas中使用命令monthly[monthly.columns[:3]].resample('Q').sum()
,我创建了一个如下所示的DataFrame:
DateIndex C1 C2 C3
2012-09-30 94 139 181
2012-12-31 111 236 162
2013-03-31 113 321 259
2013-06-30 96 238 219
然后,我可以使用命令monthly[monthly.columns[:3]].resample('Q').sum().plot(kind='bar',stacked=True)
绘制这些数字,以生成如下所示的数字:
我无法修复x轴日期标签,因此它们并不那么难看。
使用Matplotlib的.bar
函数,描述here,可能是更好的选择。但我无法确定如何将DateIndex设置为正确的格式,以便.plot
接受x
和y
变量。 Matplotlib也有useful page about date labels,但这些命令似乎不适用于Pandas .plot
函数。你能帮我吗?我已经在这里寻找过,但到目前为止还没有找到适合我的解决方案。谢谢!
答案 0 :(得分:1)
y = monthly[monthly.columns[:3]].resample('Q').sum()
fig, ax = plt.subplots(figsize=(12,5))
y.plot(ax=ax,kind='bar',stacked=True)
ax.xaxis.set_ticklabels(y.index.to_period('Q'))