formatting timeseries x-axis in pandas/matplotlib

后端 未结 2 756
刺人心
刺人心 2021-01-24 14:52

I would like to show each month abbreviation, as well as the year on the year.

I am quite close. The issue I am currently having is that the years are incorrect. I have

2条回答
  •  不要未来只要你来
    2021-01-24 15:14

    After some playing around it seems to work if you specify the axis and then plot onto that (rather than call the pandas plot function).

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    
    from matplotlib.dates import MonthLocator, WeekdayLocator, DateFormatter, YearLocator
    
    indx = pd.date_range('2017-04-01', '2019-01-01')
    s = pd.Series(np.random.randn(len(indx)), index=indx)
    
    df = pd.DataFrame(s)
    
    fig, ax = plt.subplots(1)
    ax.plot(df)
    
    months = MonthLocator(range(1, 13), bymonthday=1, interval=1)
    monthsFmt = DateFormatter("%b")
    years = YearLocator(1, month=4, day=1)
    yrsFmt = DateFormatter("\n %Y")
    
    ax.xaxis.set_major_locator(years)
    ax.xaxis.set_major_formatter(yrsFmt)
    
    
    ax.xaxis.set_minor_locator(months)
    ax.xaxis.set_minor_formatter(monthsFmt)
    
    fig.show()
    

    Also note that I changed %y to %Y so it is formatted as 2017/2018 rather than 17/18.

提交回复
热议问题