我有一个带有日期列(RankingDate
)的pandas数据框。
此日期字段最初是从csv加载的字符串,格式为“2006-11-03”
运行df["RankingDate"]=pd.to_datetime(df["RankingDate"])
后,数据类型变为'<M8[ns]'
然后我使用seaborn绘制多条线:
f, ax = plt.subplots(figsize=(16, 8))
sns.tsplot(data, time='RankingDate', unit='Dummy', condition='Player', value='Points', ax=ax)
然而,这给了我一个图表,其中日期轴以纳秒标记(即1e10 ^ 18),而不是像“2006-11-03”那样漂亮的日期格式。
如何让seaborn显示日期而不是纳秒?
示例代码:
import numpy as np
import pandas as pd
import seaborn as sns
RankingDate = ['2015-03-02','2015-03-03','2015-03-04','2015-03-05','2015-03-06']
Player = ['Player1','Player2','Player2','Player1','Player1']
Points = np.random.randn(5)
df = pd.DataFrame({'RankingDate': RankingDate , 'Player': Player, 'Points': Points})
df["RankingDate"]=pd.to_datetime(df["RankingDate"])
df["Dummy"]=0
f, ax = plt.subplots(figsize=(16, 8))
sns.tsplot(df, time='RankingDate', unit='Dummy', condition='Player', value='Points', ax=ax)