【问题标题】:pandas area plot interpolation / step style熊猫区域图插值/阶梯式
【发布时间】:2015-04-22 20:56:59
【问题描述】:

有没有办法禁用熊猫区域图中的插值? 我想得到一个“阶梯式”区域图。 例如。在法线图中可以指定:

import pandas as pd
df = pd.DataFrame({'x':range(10)})
df.plot(drawstyle = 'steps') # this works
#df.plot(kind = 'area', drawstyle = 'steps') # this does not work

我正在使用 python 2.7 和 pandas 0.14.1。

非常感谢。

【问题讨论】:

  • 嘿,我想出了我最初建议的解决方法。我不认为你会得到更好的内置功能。

标签: python python-2.7 pandas


【解决方案1】:

最近.fill_between() 中的update 附带ma​​tplotlib 1.5.0 版,允许填充两个步进函数之间的区域。这甚至适用于 Pandas 时间序列。

由于参数step='pre'存在,所以可以这样使用:

# For error bands around a series df
ax.fill_between(df.index, df - offset, df + offset, step='pre', **kwargs)

# For filling the whole area below a function
ax.fill_between(df.index, df, 0, step='pre', **kwargs)

其他对我有意义的关键字参数例如是alpha=0.3lw=0

【讨论】:

    【解决方案2】:

    据我所知,df.plot(drawstyle="steps") 甚至不存储计算的步长顶点;

    out = df.plot(kind = 'line', drawstyle = 'steps') # stepped, not filled
    stepline = out.get_lines()[0]
    print(stepline.get_data())
    

    (array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))

    所以我认为你必须自己动手。这只是在点列表中的每个(x[i],y[i]) 之后直接插入(x[i+1],y[i])

    df = pd.DataFrame({'x':range(10)})
    x = df.x.values
    xx = np.array([x,x])
    xx.flatten('F')
    doubled = xx.flatten('F') # NOTE! if x, y weren't the same, need a yy
    plt.fill_between(doubled[:-1], doubled[1:], label='area')
    ax = plt.gca()
    df.plot(drawstyle = 'steps', color='red', ax=ax) 
    

    【讨论】:

    • 我不知道 Pandas 是否可以绘制它,但 matplotlib 可以使用 Pandas 时间序列数据框来绘制。新问题?
    【解决方案3】:

    在面积上这是不可能的,但你可以使用堆积条形图来得到你想要的

    import pandas as pd
    df = pd.DataFrame({'x':range(10)})
    df.plot(kind="bar",stacked=True,width = 1)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-12
      • 2018-02-11
      • 1970-01-01
      • 1970-01-01
      • 2016-08-31
      • 2019-07-27
      相关资源
      最近更新 更多