4

Looking for confirmation or correction. It appears to me, that as long as I do this:

from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()

then I can pass a Pandas DatetimeIndex (which contains Pandas dates of type timestamps.Timestamp) directly in as the x-coordinate for Axes.plot like this:

In [4]: df
Out[4]:
           Open     High      Low    Close      Volume  AdjClose
Date
2015-12-24  2063.52  2067.36  2058.73  2060.99  1411860000   2060.99
2015-12-23  2042.20  2064.73  2042.20  2064.29  3484090000   2064.29
2015-12-22  2023.15  2042.74  2020.49  2038.97  3520860000   2038.97
2015-12-21  2010.27  2022.90  2005.93  2021.15  3760280000   2021.15
2015-12-18  2040.81  2040.81  2005.33  2005.55  6683070000   2005.55

In [5]: type(df.index)
Out[5]: pandas.core.indexes.datetimes.DatetimeIndex

In [6]: type(df.index[0])
Out[6]: pandas._libs.tslibs.timestamps.Timestamp

...

In [11]: ax.plot( df.index, df['Close'] )

and the x-axis dates work fine. But when building a plot directly using

ax.add_line(lines)

where lines[] is a list of

Line2D(xdata,ydata)

items, (for example, as can be seen here: https://github.com/matplotlib/mpl_finance/blob/master/mpl_finance.py#L133-L154 ) then the xdata must already be converted to matplotlib dates (floats as number of days since 01/01/01) doing something like this:

xdata = mdates.date2num(df.index.to_pydatetime())

Is this correct that the ax.plot() automatically converts Pandas dates, but the lower level APIs do not? Or am I missing something?


Also, to add something to this (based on the first couple comments) ... If I don't register the converters, I get this warning:

In [7]: ax.plot( df.index, df['Close'])
/anaconda3/lib/python3.6/site-packages/pandas/plotting/_converter.py:129: FutureWarning: Using an implicitly registered datetime converter for a matplotlib plotting method. The converter was registered by pandas on import. Future versions of pandas will require you to explicitly register matplotlib converters.

To register the converters:
    >>> from pandas.plotting import register_matplotlib_converters
    >>> register_matplotlib_converters()
  warnings.warn(msg, FutureWarning)

Out[7]: [<matplotlib.lines.Line2D at 0x12a437f98>]

I'm a little confused by the last line (Out[7]) ... does that mean that the code was inside Line2D when this warning was printed?

3
  • 1
    pandas.pydata.org/pandas-docs/stable/reference/api/… says that the registration is done in matplotlib's converter list. I would imagine that the lack of conversion in Line2D(x, y) would not be the fault of pandas, but rather, the failure of Line2D to convert its inputs. Commented Aug 9, 2019 at 21:15
  • 2
    Yes you are correct. Line2D expects floats. Commented Aug 9, 2019 at 21:17
  • @ifly6 Perhaps that is so. Thanks for that link. I've been having trouble finding any documentation about this (thus the reason for my question) and only noticed this by experimenting. Commented Aug 9, 2019 at 21:18

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.