【发布时间】:2016-09-29 15:53:15
【问题描述】:
在 Pandas 中,我有一个按日期时间索引并按日期分组的观察数据框(婴儿奶瓶喂养量):
...
bottles = bottles.set_index('datetime')
bottles = bottles.groupby(bottles.index.date)
我想使用 matplotlib 绘制每天增加的累积值——也就是说,显示每天增加的喂食量并在午夜重置:
ax = plt.gca()
ax.xaxis.set_major_locator(mdates.DayLocator())
ax.xaxis.set_minor_locator(mdates.HourLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))
bottles['volume'].cumsum().plot(kind='bar', figsize=[16,8])
ax.xaxis.grid(True, which="major")
ax.xaxis.grid(False, which="minor")
ax.yaxis.grid(True)
plt.gcf().autofmt_xdate()
plt.show()
我只想每天在 x 轴上标记一次日期,并且我还想只在日期边界上绘制一条垂直网格线(每 24 小时)。有关如何修复上述代码的任何建议?
【问题讨论】:
-
pandas 假定条形图是分类图,因此您总是会为显示的每个条形图打勾。我的猜测是,您将不得不直接针对 matplotlib 对象编写绘图,而不是通过 pandas 的界面。
标签: python pandas matplotlib