简体   繁体   中英

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?

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. 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)

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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