Skip to content

BUG/VIS: can't add a line with DatetimeIndex/datetime64 as x-values on timeseries plot #9053

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
jorisvandenbossche opened this issue Dec 11, 2014 · 1 comment
Labels

Comments

@jorisvandenbossche
Copy link
Member

If you do a time series plot:

s = pd.Series(np.random.randn(100), index=pd.date_range('1970-01-02', periods=100, freq='T'))
fig, ax = plt.subplots()
s.plot(ax=ax)

and then want to add a line through matplotlib using the index or datetime64 values, this does not work (it plots the data at a totally wrong place outside of sight in this case):

ax.plot(s.index, s.values, color='g')
# or
ax.plot(s.index.values, s.values, color='r')

This is because PeriodConverter cannot deal with datetime64 values (Period/PeriodIndex also cannot deal with them), while when plotting this through the pandas plot interface, the datetime64 values will first be converted to periods.

@jorisvandenbossche
Copy link
Member Author

OK, I thought this was an easy fix (#18468), but apparently it is not because matplotlib does not respect the ax.xaxis.converter.

So the first s.plot(ax=ax) sets ax.xaxis.converter to PeriodConverter. I thought that letting PeriodConverter handle datetime64 data this would solve the above issue, but when doing a ax.plot(..) apparenlty does not use the set converter, but overwrites it with a new converter:

In [16]: s = pd.Series(np.random.randn(100), index=pd.date_range('1970-01-02', periods=100, freq='T'))
    ...: fig, ax = plt.subplots()
    ...: s.plot(ax=ax)
Out[16]: <matplotlib.axes._subplots.AxesSubplot at 0x7f326f52def0>

In [17]: ax.xaxis.converter
Out[17]: <pandas.plotting._converter.PeriodConverter at 0x7f3275a03c88>

In [19]: ax.plot(s.index, s.values, color='g')
Out[19]: [<matplotlib.lines.Line2D at 0x7f326bca04a8>]

In [20]: ax.xaxis.converter
Out[20]: <pandas.plotting._converter.DatetimeConverter at 0x7f3275a03c50>   <--- changed !!

Given that, this is not something we can solve in pandas.

I opened a PR with the small fix and xfailing test, as the actual fix is useful anyway IMO (#18468)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant