简体   繁体   English

使用 seaborn 在 x 轴上绘制带有日期数据的多列

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

I have a pandas data frame with multiple columns that contain dates and one column with labels (string).我有一个 pandas 数据框,其中多列包含日期,一列带有标签(字符串)。

I want to plot the labels on the y-axis and the corresponding dates on the x-axis for all the columns.我想 plot y 轴上的标签和 x 轴上所有列的相应日期。 When I plot one it looks ok but then when I start adding the other columns it overplots the new data points and misses up the x-axis ticks.当我使用 plot 时,它看起来还可以,但是当我开始添加其他列时,它会覆盖新数据点并错过 x 轴刻度。

Anyone knows what is causing this?有谁知道这是什么原因造成的?

Here is a sample这是一个示例

Code代码 2011 2011 2012 2012 2013 2013 2014 2014 2015 2015 2016 2016 年
A1 A1 8/1/2011 2011 年 8 月 1 日 7/20/2012 2012 年 7 月 20 日 7/10/2013 2013 年 7 月 10 日 6/28/2014 2014 年 6 月 28 日 6/18/2015 2015 年 6 月 18 日 6/6/2016 2016 年 6 月 6 日
A2 A2 8/1/2011 2011 年 8 月 1 日 7/20/2012 2012 年 7 月 20 日 7/10/2013 2013 年 7 月 10 日 6/28/2014 2014 年 6 月 28 日 6/18/2015 2015 年 6 月 18 日 6/6/2016 2016 年 6 月 6 日
A3 A3 7/10/2013 2013 年 7 月 10 日 6/29/2014 2014 年 6 月 29 日 6/18/2015 2015 年 6 月 18 日 6/6/2016 2016 年 6 月 6 日
A4 A4 7/10/2013 2013 年 7 月 10 日 6/28/2014 2014 年 6 月 28 日 6/18/2015 2015 年 6 月 18 日 6/6/2016 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 is stronger with long form data. Seaborn 对于长格式数据更强。 So you should melt then plot:所以你应该融化然后plot:

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

Output: Output:

在此处输入图像描述

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

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