【问题标题】:Make datetime line look nice on seaborn plot x axis使日期时间线在 seaborn 绘图 x 轴上看起来不错
【发布时间】:2020-06-08 13:16:51
【问题描述】:

您如何将日期时间重新格式化为第 1 周、第 2 周...以绘制到 seaborn 折线图上?

输入

         Date     Ratio
0  2019-10-04  0.350365
1  2019-10-04  0.416058
2  2019-10-11  0.489051
3  2019-10-18  0.540146
4  2019-10-25  0.598540
5  2019-11-08  0.547445
6  2019-11-01  0.722628
7  2019-11-15  0.788321
8  2019-11-22  0.875912
9  2019-11-27  0.948905

期望的输出

我能够通过将数据框的自然索引与周匹配来解决这个问题。我想知道是否有其他方法可以做到这一点。

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

data = {'Date': ['2019-10-04',
                 '2019-10-04',
                 '2019-10-11',
                 '2019-10-18',
                 '2019-10-25',
                 '2019-11-08',
                 '2019-11-01',
                 '2019-11-15',
                 '2019-11-22',
                 '2019-11-27'],
        'Ratio':       [0.350365,
                        0.416058,
                        0.489051,
                        0.540146,
                        0.598540,
                        0.547445,
                        0.722628,
                        0.788321,
                        0.875912,
                        0.948905]}

df = pd.DataFrame(data)
df['Date'] = pd.to_datetime(df['Date'])
graph = sns.lineplot(data=df,x='Date',y='Ratio')

plt.show()
# First plot looks bad.

week_mapping = dict(zip(df['Date'].unique(),range(len(df['Date'].unique()))))

df['Week'] = df['Date'].map(week_mapping)
graph = sns.lineplot(data=df,x='Week',y='Ratio')

plt.show()
# This plot looks better, but method seems cheesy.

【问题讨论】:

    标签: python pandas time-series seaborn


    【解决方案1】:

    您的数据似乎已经每周间隔,所以您可以这样做:

    df.groupby('Date',as_index=False)['Ratio'].mean().plot()
    

    输出:

    【讨论】:

      【解决方案2】:

      您可以使用周数创建一个新列并将其用作您的 x 值。这会给你一年中的一周。如果你想用 0 开始你的周数,只需从值中减去第一个日期的周数(参见代码的注释掉部分)

      import pandas as pd
      import seaborn as sns
      import matplotlib.pyplot as plt
      from datetime import datetime as dt
      
      data = {'Date': ['2019-10-04',
                       '2019-10-04',
                       '2019-10-11',
                       '2019-10-18',
                       '2019-10-25',
                       '2019-11-08',
                       '2019-11-01',
                       '2019-11-15',
                       '2019-11-22',
                       '2019-11-27'],
              'Ratio':       [0.350365,
                              0.416058,
                              0.489051,
                              0.540146,
                              0.598540,
                              0.547445,
                              0.722628,
                              0.788321,
                              0.875912,
                              0.948905]}
      
      df = pd.DataFrame(data)
      df['Date'] = pd.to_datetime(df['Date'])
      # To get the week number of the year
      df.loc[:, 'Week'] = df['Date'].dt.week
      # Or you can use the line below for the exact output you had
      #df.loc[:, 'Week'] = df['Date'].dt.week - (df.sort_values(by='Date').iloc[0,0].week)
      graph = sns.lineplot(data=df,x='Week',y='Ratio')
      
      plt.show()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-03
        • 2021-09-27
        • 2017-12-30
        • 2016-06-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多