我正在使用pandas
和matplotlib
来绘制数据帧时间序列,其中类别列的每个“分组依据”应绘制为带“ min /最大”。
时间序列是一系列日期时间对象,它们代表一个季度的开始,并且也是数据帧的索引。
有时候,即使我尝试使用ValueError: ordinal must be >= 1
和matplotlib
之类的命令从以前的绘图中清除plt.clf()
的历史记录,我也会得到plt.close('all')
。
我认为问题可能与轴的重复使用有关,但是如果这是根本原因,那么我不确定如何避免这种情况。
有时我在KeyError
字段上得到datetime
(我也使用命令pair_filtered_df.set_index('year_quarter', inplace=True)
进行了索引,并在使用加载CSV文件时解析datetime
pd.read_csv
和参数parse_dates=['year_quarter']
)
这是代码:
fig, ax = plt.subplots()
for key, grp in pair_filtered_df.groupby(['category']):
# trying to plot only for one category
# in an attempt to isolate/reproduce the error
if key == "a":
ax = grp.plot(ax=ax, kind='line', x='year_quarter', y='median', label=key)
# according to some examples I've see this
# `fill_between` should allow plotting
# a "min/max" band wrapping the "median"
plt.fill_between(grp.index, grp['min'], grp['max'], color='b', alpha=0.2)
运行pair_filtered_df.head()
时,我看到了数据框的这种结构:
运行pair_filtered_df.tail()
时,我看到了数据框的这种结构:
我找不到解决此问题的方法,主要是因为我不完全了解如何使用pandas
处理索引以及plt
的{{1}}事物是如何工作的”做隐藏的事情”,但我不断看到许多不同的错误。
如何绘制按类别和使用最小/最大波段细分的时间序列?
如何为最小/最大带和中位数绘制具有不同“家庭颜色”的不同类别?