Plotting datetimeindex on x-axis with matplotlib creates wrong ticks in pandas 0.15 in contrast to 0.14

前端 未结 2 497
梦谈多话
梦谈多话 2020-12-29 10:14

I create a simple pandas dataframe with some random values and a DatetimeIndex like so:

import pandas as pd
from numpy.random import randint
import datetime          


        
2条回答
  •  春和景丽
    2020-12-29 10:44

    Note that this bug was fixed in pandas 0.15.1 (https://github.com/pandas-dev/pandas/pull/8693), and plt.plot(df.index, df.RandomValues) now just works again.


    The reason for this change in behaviour is that starting from 0.15, the pandas Index object is no longer a numpy ndarray subclass. But the real reason is that matplotlib does not support the datetime64 dtype.

    As a workaround, in the case you want to use the matplotlib plot function, you can convert the index to python datetime's using to_pydatetime:

    plt.plot(df.index.to_pydatetime(), df.RandomValues)
    

    More in detail explanation:

    Because Index is no longer a ndarray subclass, matplotlib will convert the index to a numpy array with datetime64 dtype (while before, it retained the Index object, of which scalars are returned as Timestamp values, a subclass of datetime.datetime, which matplotlib can handle). In the plot function, it calls np.atleast_1d() on the input which now returns a datetime64 array, which matplotlib handles as integers.

    I opened an issue about this (as this gets possibly a lot of use): https://github.com/pydata/pandas/issues/8614

提交回复
热议问题