使用seaborn保留工作日在熊猫箱图上订购[复制]

问题描述 投票:0回答:1

我有一个简单的数据集,上面有几天:

dt, value, coltype
2017-01-01, 10, A 
2017-01-02, 11, B
2017-01-03, 30, A
2017-01-04, 90, C
2017-01-05, 9,  A
2017-01-06, 13, E
2017-01-07, 12, C
2017-01-08, 10, B

我想基于工作日创建一个简单的盒子图:

import seaborn as sns
import pandas as pd

df = read_csv('mycsv.txt')
df.index = pd.to_datetime(df.dt)
sns.boxplot(x=df.index.weekday_name, y=value)

我得到的是一个箱形图,但没有订购周日:boxplot

有没有办法在boxplot函数上直接进行,而不创建另一列?

python pandas matplotlib seaborn
1个回答
1
投票

使用order参数:

order = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
sns.boxplot(x=df.index.weekday_name, y=df.value, order=order)

如果您的日期已完成且包含每个工作日的数据,您将获得以下内容:

enter image description here

如果说您没有其中一个工作日的数据并且仍然调用相同的代码,您会得到类似的结果:

enter image description here

这完全没问题(至少对我而言),你也可以清楚地看到星期二没有数据,这也会告诉你更多关于你的数据的信息。

© www.soinside.com 2019 - 2024. All rights reserved.