小提琴情节:一把小提琴,布尔值分为两半

Violin plot: one violin, two halves by boolean value

我正在玩弄 seaborn violinplot,试图制作一个 "violin" 每一半都是不同的分布,以便于比较。

通过将 x 轴更改为 x=smoker 来修改 here 中的简单示例,我得到了下图(链接如下)。

import seaborn as sns
sns.set(style="whitegrid", palette="pastel", color_codes=True)

# Load the example tips dataset
tips = sns.load_dataset("tips")

# Draw a nested violinplot and split the violins for easier comparison
sns.violinplot(x="smoker", y="total_bill", hue="smoker",
               split=True, inner="quart", data=tips)
sns.despine(left=True)

This is the resulting graph

我希望图表没有显示两个分开的部分,只有一把小提琴有两种不同的分布和颜色。

seaborn 可以做到这一点吗?或者与其他图书馆合作?

谢谢!

这是因为您使用此行 x="smoker" 为 x 轴指定了两件事。也就是说,它绘制了 smoker yes 和 smoker no。

您真正想做的是绘制所有数据。为此,您只需为 x 轴指定一个值即可。

sns.set(style="whitegrid", palette="pastel", color_codes=True)

# Load the example tips dataset
tips = sns.load_dataset("tips")

# Draw a nested violinplot and split the violins for easier comparison
sns.violinplot(x=['Data']*len(tips),y="total_bill", hue="smoker",
               split=True, inner="quart",
               palette={"Yes": "y", "No": "b"},
               data=tips)
sns.despine(left=True)

这会输出以下内容: