123*_*3py 6 python python-2.7 pandas
有没有办法禁用熊猫区域图中的插值?我想得到一个“阶梯式”面积图。例如,在法线图中可以指定:
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
Run Code Online (Sandbox Code Playgroud)
我正在使用 python 2.7 和 Pandas 0.14.1。
提前谢谢了。
最近, matplotlib 版本 1.5.0发布了一个更新,允许填充两个步骤函数之间的区域。这甚至适用于 Pandas 时间序列。.fill_between()
由于参数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)
Run Code Online (Sandbox Code Playgroud)
对我来说有意义的其他关键字参数例如alpha=0.3and lw=0。
据我所知,df.plot(drawstyle="steps")甚至不存储计算出的步骤顶点;
out = df.plot(kind = 'line', drawstyle = 'steps') # stepped, not filled
stepline = out.get_lines()[0]
print(stepline.get_data())
Run Code Online (Sandbox Code Playgroud)
(数组([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), 数组([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) )
所以我认为你必须自己动手。这只是直接插入点列表中的(x[i+1],y[i])每个之后:(x[i],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)
Run Code Online (Sandbox Code Playgroud)
