Pandas区地块插值/台阶

2024-09-28 01:33:46 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有办法禁用熊猫区的插值? 我想得到一个“台阶式”面积图。 E、 g.在正常线图中,可以指定:

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

我使用的是python2.7和pandas 0.14.1。在

提前致谢。在


Tags: importdataframepandasdfplotasstepsthis
3条回答

据我所知,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])

^{pr2}$

enter image description here

最近,.fill_between()updatematplotlib 1.5.0版一起提供,允许填充两个步骤函数之间的区域。这甚至适用于熊猫时间序列。在

由于参数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.3和{}。在

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

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

enter image description here

相关问题 更多 >

    热门问题