Plotting of DatetimeIndex directly with matplotlib no longer gives datetime formatted axis (0.15) · Issue #8614 · pandas-dev/pandas (original) (raw)

From http://stackoverflow.com/questions/26526230/plotting-datetimeindex-on-x-axis-with-matplotlib-creates-wrong-ticks-in-pandas-0

df = pd.DataFrame({'RandomValues': np.random.randint(1, 50, 60)},
                  index=pd.date_range("2012-01-01", periods=60))

Plotting of course works:

In [79]: df.plot()
Out[79]: <matplotlib.axes._subplots.AxesSubplot at 0xf571550>

But plotting with matplotlib's plot functions gives no longer a datetime formatted x-axis, but just ints:

In [80]: plt.plot(df.index, df.RandomValues)
Out[80]: [<matplotlib.lines.Line2D at 0xfb5f748>]

Reason: matplotlibs plot calls np.atleast_1d on the input (https://github.com/matplotlib/matplotlib/blob/v1.4.0/lib/matplotlib/axes/_base.py#L220). Previously this returned an Index, now it returns a array of datetime64 values, which matplotlib can't handle.

Issues:

It seems there was some discussion about this in the Index-no-subclass PR: #7891 (comment) and #7891 (comment) (I commented there about exactly this issue, but I don't know why we didn't look further in it then)