Skip gcf().autofmt_xdate() at pandas plot creation

前端 未结 1 545
名媛妹妹
名媛妹妹 2021-02-10 19:49

I\'m trying to plot multiple time series using a pandas dataframe. The dataframe contains more than 100 registers.

From the panda\'s documentation I\'ve read that when p

1条回答
  •  名媛妹妹
    2021-02-10 20:22

    I think you can just clear all the ticks before you make new ones:

    df=pd.DataFrame({'A':np.random.random(20), 'B':np.random.random(20)})
    df.index=pd.date_range('1/1/2014', periods=20, freq='5H')
    ax = plt.figure(figsize=(7,5), dpi=100).add_subplot(111)
    df.plot(ax=ax)
    ax.set_xticks([])
    ax.set_xticks([], minor=True)
    ax.xaxis.set_major_locator(mdates.HourLocator(interval=200))
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%m/%d-%H:%S'))
    ax.xaxis.grid(True, which="minor")
    plt.xticks(rotation='vertical', fontsize = 8)
    plt.subplots_adjust(bottom=.2)
    

    enter image description here

    Edit

    Now the labels are off. ax.xaxis.set_major_formatter(mdates.DateFormatter('%y/%m/%d-%H:%S')) will show they are 1991/01/11 and so on.

    df=pd.DataFrame({'A':np.random.random(20), 'B':np.random.random(20)})
    df.index=pd.date_range('1/1/2014', periods=20, freq='5H')
    ax = plt.figure(figsize=(7,5), dpi=100).add_subplot(111)
    ax.plot(df.index.to_pydatetime(), df.A, label='A')
    ax.plot(df.index.to_pydatetime(), df.B, label='B')
    ax.legend()
    ax.xaxis.set_major_locator(mdates.HourLocator(interval=5))
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%y/%m/%d-%H:%S'))
    ax.xaxis.grid(True, which="major")
    ax.yaxis.grid(True, which="major")
    plt.xticks(rotation='vertical', fontsize = 8)
    plt.subplots_adjust(bottom=.2)
    

    enter image description here

    0 讨论(0)
提交回复
热议问题