【问题标题】:Seaborn violin plot - compare all to oneSeaborn 小提琴情节 - 全部比较
【发布时间】:2019-09-30 08:56:31
【问题描述】:

我有一个类似于 Seaborn 文档制作的以下情节:https://seaborn.pydata.org/generated/seaborn.violinplot.html

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

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

这可以通过 Seaborn 的内置功能实现吗?

【问题讨论】:

    标签: python matplotlib seaborn


    【解决方案1】:

    我认为没有任何“内置”功能可以满足您的需求,但如果您不介意对数据框进行一些创意,这很容易做到,但您必须提供确保数据框的结构。

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

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-22
      • 2019-05-21
      • 2021-02-16
      • 2017-01-02
      • 2023-03-31
      • 2021-08-29
      • 2018-05-15
      • 2018-05-15
      相关资源
      最近更新 更多