简体   繁体   English

使日期时间线在 seaborn 图 x 轴上看起来不错

[英]Make datetime line look nice on seaborn plot x axis

How do you reformat from datetime to Week 1, Week 2... to plot onto a seaborn line chart?您如何将日期时间重新格式化为第 1 周、第 2 周...以绘制到 seaborn 折线图上?

Input输入

         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

Desired output期望输出在此处输入图片说明

I was able to cheese it by matching the natural index of the dataframe to the week.我能够通过将数据框的自然索引与周匹配来解决它。 I wonder if there's another way to do this.我想知道是否有另一种方法可以做到这一点。

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.

It looks like your data is already spaced weekly, so you can just do:看起来您的数据已经每周间隔一次,因此您可以执行以下操作:

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

Output:输出:

在此处输入图片说明

You can make a new column with the week number and use that as your x value.您可以使用周数创建一个新列,并将其用作 x 值。 This would give you the week of the year.这会给你一年中的一周。 If you want to start your week numbers with 0, just subtract the week number of the first date from the value (see the commented out section of the code)如果你想用 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM