Lamps1829
Lamps1829

Reputation: 2451

Can't pass pandas Series to pyplot's fill_between function?

Passing a pandas Series to pyplot's fill_between() function produces the following error:

<type 'exceptions.KeyError'> -1

However, other pyplot functions seem to be fine with taking Series as inputs (plot() and scatter() seem to work fine with it). I know this issue can be fixed by passing my_series.value instead of my_series, but can someone explain why some pyplot plotting functions seem to be ok with getting a Series as input, while others are not? Which functions will crash when given a Series? Thanks.

Upvotes: 1

Views: 1040

Answers (2)

tacaswell
tacaswell

Reputation: 87486

Because matplotlib is written to take sequence or np.ndarray-like objects as arguments (and knows nothing about pandas). In the cases where all of the methods used internally work the same on pandas objects and numpy objects, then it works (the magic of duck typing). In cases where pandas objects do not behave correctly (in this case using v[-1] to get the last element of the first dimension out) it will raise errors.

If a given function works with the pandas objects depends on the internals of the function and is not guaranteed to be stable even between minor releases of mpl because you are essentially using matplotlib in an undocumented way.

Upvotes: 2

Ashish
Ashish

Reputation: 739

Till pandas is not officially implemented in plt.fill_between function, you can still apply pd.Series or pd.DataFrame as pd.Series().values and pd.DataFrame().values to make fill_between plots.

Upvotes: 2

Related Questions