在Matplotlib

时间:2017-11-15 01:35:39

标签: python pandas matplotlib

在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)绘制这些数字,以生成如下所示的数字:

Screenshot of resampled quarterly bar plot

我无法修复x轴日期标签,因此它们并不那么难看。

使用Matplotlib的.bar函数,描述here,可能是更好的选择。但我无法确定如何将DateIndex设置为正确的格式,以便.plot接受xy变量。 Matplotlib也有useful page about date labels,但这些命令似乎不适用于Pandas .plot函数。你能帮我吗?我已经在这里寻找过,但到目前为止还没有找到适合我的解决方案。谢谢!

1 个答案:

答案 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'))