pandas .plot() x-axis tick frequency — how can I show more ticks?

后端 未结 4 1834
梦毁少年i
梦毁少年i 2020-12-09 04:44

I am plotting time series using pandas .plot() and want to see every month shown as an x-tick.

Here is the dataset structure

Here is the result of the .plo

4条回答
  •  -上瘾入骨i
    2020-12-09 05:06

    No need to pass any args to MonthLocator. Make sure to use x_compat in the df.plot() call per @Rotkiv's answer.

    import pandas as pd
    import numpy as np
    import matplotlib.pylab as plt
    import matplotlib.dates as mdates
    
    df = pd.DataFrame(np.random.rand(100,2), index=pd.date_range('1-1-2018', periods=100))
    ax = df.plot(x_compat=True)
    ax.xaxis.set_major_locator(mdates.MonthLocator())
    plt.show()
    

提交回复
热议问题