首页 > 解决方案 > Seaborn 小提琴情节 - 全部比较

问题描述

我有一个由 Seaborn 文档制作的如下图:https ://seaborn.pydata.org/generated/seaborn.violinplot.html

在此处输入图像描述

让我们不要介意这里的实际数据和标签,它只是为了演示。我想做的是从相同的数据(作为基线)创建所有橙色分布。

因此,在这种情况下,每个工作日的所有蓝色分布都将针对相同的橙色分布进行绘制。

Seaborn 的内置功能可以做到这一点吗?

标签: pythonmatplotlibseaborn

解决方案


我不认为有任何“内置”可以做你想做的事,但如果你不介意对你的数据框有一点创意,这很容易做到,但你必须提供你的结构数据框以确保。

这是使用tipsseaborn 的数据集的一种(非常低效的)方法

target_var = 'total_bill'
hue_var = 'smoker'
hue_value = 'Yes'
cat_var = 'day'
grouped_value = 'ALL WEEK'

tips = sns.load_dataset("tips")
tips2 = tips.loc[tips[hue_var]==hue_value]
for cat in tips[cat_var].unique():
    temp = tips.loc[:,[target_var]]
    temp[cat_var] = cat
    temp[hue_var] = grouped_value
    tips2 = tips2.append(temp, ignore_index=True, sort=False)

fig, (ax1, ax2) = plt.subplots(1,2, figsize=(10,4))
sns.violinplot(x=cat_var, y=target_var, hue=hue_var, hue_order=['Yes','No'], 
               order=['Thur','Fri','Sat','Sun'],
               data=tips, palette="muted", split=True, ax=ax1)
sns.violinplot(x=cat_var, y=target_var, hue=hue_var, hue_order=[hue_value,grouped_value], 
               order=['Thur','Fri','Sat','Sun'],
               data=tips2, palette="muted", split=True, ax=ax2)

在此处输入图像描述


推荐阅读