You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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)
If you do a time series plot:
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):
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.The text was updated successfully, but these errors were encountered: