Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas area plot interpolation / step style

Is there a way to disable the interpolation in the pandas area plot? I would like to get a "step-style" area plot. E.g. in the normal line plot it is possible to 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.

Many thanks in advance.

like image 271
123py Avatar asked Apr 22 '15 20:04

123py


People also ask

Are area plots stacked by default?

By default uses the index. Column to plot. By default uses all columns. Area plots are stacked by default.

How do pandas interpolate missing values?

You can interpolate missing values ( NaN ) in pandas. DataFrame and Series with interpolate() . This article describes the following contents. Use dropna() and fillna() to remove missing values NaN or to fill them with a specific value.


1 Answers

Recently an update of .fill_between() was shipped with matplotlib version 1.5.0 that allows to fill the area between two step functions. This even works with Pandas time series.

Since the argument step='pre' exists, 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 key word arguments that make sense to me are for example alpha=0.3 and lw=0.

like image 61
Martin Avatar answered Sep 21 '22 04:09

Martin