Skip to content
Advertisement

Matplotlib’s fill_between doesnt work with plot_date, any alternatives?

I want to create a plot just like this: This has integer x values

The code:

JavaScript

but with dates in the x axis, like this (without bands): with plot_date

the code:

JavaScript

the problem is that fill_between fails when x values are date_time objects.

Does anyone know of a workaround? DF is a pandas DataFrame.

Advertisement

Answer

It would help if you show how df is defined. What does df.info() report? This will show us the dtypes of the columns.

There are many ways that dates can be represented: as strings, ints, floats, datetime.datetime, NumPy datetime64s, Pandas Timestamps, or Pandas DatetimeIndex. The correct way to plot it depends on what you have.

Here is an example showing your code works if df.index is a DatetimeIndex:

JavaScript

enter image description here


If the index has string representations of dates, then (with Matplotlib version 1.4.2) you would get a TypeError:

JavaScript

yields

JavaScript

In this case, the fix is to convert the strings to Timestamps:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement