Compare multiple year data on a single plot python

后端 未结 3 1390
甜味超标
甜味超标 2021-01-13 04:13

I have two time series from different years stored in pandas dataframes. For example:

data15 = pd.DataFrame(
    [1,2,3,4,5,6,7,8,9,10,11,12],
    index=pd.d         


        
相关标签:
3条回答
  • 2021-01-13 04:44

    I see two options.

    Option 1: add a month column to your dataframes

    data15['month'] = data15.index.to_series().dt.strftime('%b')
    data16['month'] = data16.index.to_series().dt.strftime('%b')
    
    ax = data16.plot(x='month', y='2016')
    ax = data15.plot(x='month', y='2015', ax=ax)
    

    Option 2: if you don't want to do that, you can use matplotlib directly

    import matplotlib.pyplot as plt
    
    fig, ax = plt.subplots()
    ax.plot(data15['2015'].values)
    ax.plot(data16['2016'].values)
    plt.xticks(range(len(data15)), data15.index.to_series().dt.strftime('%b'), size='small')
    

    Needless to say, I would recommend the first option.

    0 讨论(0)
  • 2021-01-13 04:58

    One way to do this would be to overlay a transparent axes over the first, and plot the 2nd dataframe in that one, but then you'd need to update the x-limits of both axes at the same time (similar to twinx). However, I think that's far more work and has a few more downsides: you can't easily zoom interactively into a specific region anymore for example, unless you make sure both axes are linked via their x-limits. Really, the easiest is to take into account that offset, by "messing with the index".

    As for the tick labels, you can easily change the format so that they don't show the year by specifying the x-tick format:

    import matplotlib.dates as mdates
    month_day_fmt = mdates.DateFormatter('%b %d') # "Locale's abbreviated month name. + day of the month"
    ax.xaxis.set_major_formatter(month_day_fmt)
    

    Have a look at the matplotlib API example for specifying the date format.

    0 讨论(0)
  • 2021-01-13 04:58

    You might be able to use pandas.DatetimeIndex.dayofyear to get the day number which will allow you to plot two different year's data on top of one another.

    in: date=pd.datetime('2008-10-31')
    in: date.dayofyear
    out: 305
    
    0 讨论(0)
提交回复
热议问题