簡體   English   中英

使用 seaborn 在 x 軸上繪制帶有日期數據的多列

[英]plotting multiple columns with date data on the x-axis using seaborn

我有一個 pandas 數據框,其中多列包含日期,一列帶有標簽(字符串)。

我想 plot y 軸上的標簽和 x 軸上所有列的相應日期。 當我使用 plot 時,它看起來還可以,但是當我開始添加其他列時,它會覆蓋新數據點並錯過 x 軸刻度。

有誰知道這是什么原因造成的?

這是一個示例

代碼 2011 2012 2013 2014 2015 2016 年
A1 2011 年 8 月 1 日 2012 年 7 月 20 日 2013 年 7 月 10 日 2014 年 6 月 28 日 2015 年 6 月 18 日 2016 年 6 月 6 日
A2 2011 年 8 月 1 日 2012 年 7 月 20 日 2013 年 7 月 10 日 2014 年 6 月 28 日 2015 年 6 月 18 日 2016 年 6 月 6 日
A3 2013 年 7 月 10 日 2014 年 6 月 29 日 2015 年 6 月 18 日 2016 年 6 月 6 日
A4 2013 年 7 月 10 日 2014 年 6 月 28 日 2015 年 6 月 18 日 2016 年 6 月 6 日
df = pd.read_csv('dates.csv', parse_dates=['2011', '2012', '2013', '2014', '2015', '2016'])
fig, ax = plt.subplots(figsize=(10, 6))

sns.stripplot(pd.to_datetime(df['2012']), df['Code'], color = 'r')
sns.stripplot(pd.to_datetime(df['2013']), df['Code'], color = 'g')
sns.stripplot(pd.to_datetime(df['2014']), df['Code'], color = 'k')

plt.xticks(rotation='vertical')

Seaborn 對於長格式數據更強。 所以你應該融化然后plot:

ax = sns.stripplot(data=df.melt('Code', value_name='date', var_name='year'), 
                   x='date', y='Code', hue='year')

Output:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM