Seaborn regplot using datetime64 as the x axis

前端 未结 1 509
耶瑟儿~
耶瑟儿~ 2020-12-16 01:43

I have a dataframe looks like this:

date         score  
2017-06-04    90
2017-06-03    80
2017-06-02    70

When I tried this:



        
相关标签:
1条回答
  • 2020-12-16 01:53

    Seaborn doesn't support datetimes in regplot but here's an ugly hack:

    df = df.sort_values('date')
    df['date_f'] = pd.factorize(df['date'])[0] + 1
    mapping = dict(zip(df['date_f'], df['date'].dt.date))
    
    ax = sns.regplot('date_f', 'score', data=df)
    labels = pd.Series(ax.get_xticks()).map(mapping).fillna('')
    ax.set_xticklabels(labels)
    

    produces

    This is the main approach used in time-series regression. If you have daily data, you code day 1 as 1 and increase the number as the days go by. This assumes you have a regularly-spaced time series.

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