seaborn violinplot 和 boxplot 并排
seaborn violinplot and boxplot side by side
我想并排比较 seaborn vilonplot 和箱线图。
这是我的数据集样本:
group points
0 A 12.432848
1 A 13.246483
2 A 13.812403
3 A 13.212260
4 A 15.307191
5 B 13.464179
6 B 11.695743
7 B 12.197824
8 B 13.892186
9 B 9.586561
10 C 18.071026
11 C 18.522597
12 C 17.649151
13 C 18.266435
14 C 17.088155
我知道我可以用 sns.boxplot(data=df, x="group", y="points", hue="group")
得到箱线图,用 sns.violinplot(data=df, x="group", y="points", hue="group")
得到小提琴图,但我想附上它们,一半是箱线图,一半是小提琴图,如下图所示。
您没有像显示的图像那样具有三个维度。我相信你只是想要:
import pandas as pd
import seaborn as sns
example_data = (
pd.DataFrame(
[
['A', 12],
["A", 15],
["A", 18],
["B", 11],
["B", 10],
["B", 8]
]
)
)
example_data.columns = ['group', 'points']
sns.set_style('whitegrid')
sns.violinplot(
data=example_data,
x='group',
y='points'
)
输出:
我会参考 https://seaborn.pydata.org/generated/seaborn.violinplot.html 以进一步定制和记录
真的有兴趣做这个吗?小提琴图的中心已经包含了一个小箱线图。
不过,这可以通过使用假色调级别并在两个图之间切换顺序来实现:
df2 = df.assign(hue=1)
sns.boxplot(data=df2, x="group", y="points", hue="hue", hue_order=[1,0])
g = sns.violinplot(data=df2, x="group", y="points", hue="hue", split=True, hue_order=[0,1])
g.legend_.remove() # hide legend
我想并排比较 seaborn vilonplot 和箱线图。 这是我的数据集样本:
group points
0 A 12.432848
1 A 13.246483
2 A 13.812403
3 A 13.212260
4 A 15.307191
5 B 13.464179
6 B 11.695743
7 B 12.197824
8 B 13.892186
9 B 9.586561
10 C 18.071026
11 C 18.522597
12 C 17.649151
13 C 18.266435
14 C 17.088155
我知道我可以用 sns.boxplot(data=df, x="group", y="points", hue="group")
得到箱线图,用 sns.violinplot(data=df, x="group", y="points", hue="group")
得到小提琴图,但我想附上它们,一半是箱线图,一半是小提琴图,如下图所示。
您没有像显示的图像那样具有三个维度。我相信你只是想要:
import pandas as pd
import seaborn as sns
example_data = (
pd.DataFrame(
[
['A', 12],
["A", 15],
["A", 18],
["B", 11],
["B", 10],
["B", 8]
]
)
)
example_data.columns = ['group', 'points']
sns.set_style('whitegrid')
sns.violinplot(
data=example_data,
x='group',
y='points'
)
输出:
我会参考 https://seaborn.pydata.org/generated/seaborn.violinplot.html 以进一步定制和记录
真的有兴趣做这个吗?小提琴图的中心已经包含了一个小箱线图。
不过,这可以通过使用假色调级别并在两个图之间切换顺序来实现:
df2 = df.assign(hue=1)
sns.boxplot(data=df2, x="group", y="points", hue="hue", hue_order=[1,0])
g = sns.violinplot(data=df2, x="group", y="points", hue="hue", split=True, hue_order=[0,1])
g.legend_.remove() # hide legend