9

I have a timeseries in Pandas where the dates are at the end of the month:

import pandas as pd
s = pd.Series({
    '2018-04-30':     0,
    '2018-05-31':     1,
    '2018-06-30':     0,
    '2018-07-31':    1,
    '2018-08-31':    0,
    '2018-09-30':    1,
})
s.index = pd.to_datetime(s.index)

When I plot this with matplotlib I get the result I'd expect, with points at the end-of-month and the line starting just before May 2018:

import matplotlib.pyplot as plt
plt.plot(s)

Matplotlib graph

But Pandas' native plot function plots the points at the start of the month:

s.plot()

Pandas graph

I thought perhaps this was just Pandas labelling '30 April' as 'April', but that doesn't seem to be the case:

s2 = pd.Series([0.2, 0.7], index=pd.date_range('2018-05-01', '2018-05-02'))
s.plot()
s2.plot()

Pandas plot with first-of-month also plotted

Is this a bug in Pandas or am I doing something wrong here?

1 Answer 1

2

This seems to be a bug in pandas. To get the same as with matplotlib you may always use x_compat=True. This then would also allow to use matplotlib.dates formatters and locators.

s = pandas.Series(...)
s.plot(x_compat=True)
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.