如何绘制自己的最小/最大范围内的中位数(按类别细分)?

时间:2018-09-02 10:43:41

标签: python pandas matplotlib plot

我正在使用pandasmatplotlib来绘制数据帧时间序列,其中类别列的每个“分组依据”应绘制为带“ min /最大”。

时间序列是一系列日期时间对象,它们代表一个季度的开始,并且也是数据帧的索引。

有时候,即使我尝试使用ValueError: ordinal must be >= 1matplotlib之类的命令从以前的绘图中清除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()时,我看到了数据框的这种结构:

enter image description here

运行pair_filtered_df.tail()时,我看到了数据框的这种结构:

enter image description here

我找不到解决此问题的方法,主要是因为我不完全了解如何使用pandas处理索引以及plt的{​​{1}}事物是如何工作的”做隐藏的事情”,但我不断看到许多不同的错误。

如何绘制按类别和使用最小/最大波段细分的时间序列?

如何为最小/最大带和中位数绘制具有不同“家庭颜色”的不同类别?

0 个答案:

没有答案