Pandas parcel area interpolation / step style

Is there a way to disable pandas scope interpolation? I would like to get a step-by-step plot. For example. in a normal line graph, you can specify:

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

      

I am using python 2.7 and pandas 0.14.1.

Thank you very much in advance.

+4


source to share


3 answers


An update .fill_between()

was recently shipped with matplotlib version 1.5.0 , which allows padding between two step functions. It even works with Pandas time series.

Since the argument exists step='pre'

, it can be used like this:



# 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)

      

Additional keyword arguments that make sense to me like alpha=0.3

and lw=0

.

+3


source


df.plot(drawstyle="steps")

Doesn't even store the calculated step vertices as far as I can tell ;

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]) )



so i guess you will have to roll yourself. It's just pasting (x[i+1],y[i])

directly after each (x[i],y[i])

point in the list:

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) 

      

enter image description here

+2


source


with an area this is not possible, but you can use a stacked histogram to get what you want

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

      

enter image description here

0


source







All Articles