Months as axis ticks

前端 未结 1 1966
余生分开走
余生分开走 2020-12-01 22:53

I would like to add monthly ticks to this plot - and it is currently showing ticks of 2015 and 2016 where i would like it to show Jan, Feb...Dec, Jan.

The index of

相关标签:
1条回答
  • 2020-12-01 23:19

    Tough to try without your exact data, but I think you're simply missing a call to set_major_formatter.

    import datetime
    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.dates as mdates
    
    # Generate some random date-time data
    numdays = 500
    base = datetime.datetime.today()
    date_list = [base - datetime.timedelta(days=x) for x in range(0, numdays)]
    y = np.random.rand(numdays)
    
    # Set the locator
    locator = mdates.MonthLocator()  # every month
    # Specify the format - %b gives us Jan, Feb...
    fmt = mdates.DateFormatter('%b')
    
    
    plt.plot(date_list,y)
    X = plt.gca().xaxis
    X.set_major_locator(locator)
    # Specify formatter
    X.set_major_formatter(fmt)
    plt.show()
    

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