Skip to content

Plotting of DatetimeIndex directly with matplotlib no longer gives datetime formatted axis (0.15) #8614

Closed
@jorisvandenbossche

Description

@jorisvandenbossche

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:

  • This is actually a problem of matplotlib (they should support datetime64), but of course: a lot of users rely now on the behaviour that it did work with a pandas Index also.
  • Is there a way to restore this behaviour? I don't see directly a solution?
  • We should update our docs, as this behaviour is also mentioned there briefly: http://pandas.pydata.org/pandas-docs/stable/visualization.html#plotting-directly-with-matplotlib + figure out why this didn't fail in the doc build (the figure seems not updated with the 0.15 doc build)

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)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions