Python 在Matplotlib中绘制重采样时间序列数据的条形图时固定日期标签

Python 在Matplotlib中绘制重采样时间序列数据的条形图时固定日期标签,python,pandas,matplotlib,Python,Pandas,Matplotlib,使用pandas中的命令monthly[monthly.columns[:3]].resample('Q').sum(),我创建了一个如下所示的数据帧: 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]]绘制这些数字。重新采样

使用pandas中的命令
monthly[monthly.columns[:3]].resample('Q').sum()
,我创建了一个如下所示的数据帧:

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]]绘制这些数字。重新采样('Q').sum()。绘制(kind='bar',stacked=True)
生成如下图形:

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

我不能做的是修复x轴日期标签,这样它们就不会那么难看了。


使用Matplotlib的
.bar
函数(如所述)可能是一个更好的选择。但我无法确定如何将日期索引转换为正确的格式,因此
.plot
将接受
x
y
变量。Matplotlib也有一个,但这些命令似乎不适用于Pandas
.plot
函数。你能帮忙吗?我在这里四处寻找,但到目前为止还没有找到适合我的解决方案。谢谢

可以使用dt.date将日期时间转换为日期
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'))