【问题标题】:seaborn violinplot and boxplot side by sideseaborn violinplot 和 boxplot 并排
【发布时间】:2021-10-29 13:41:22
【问题描述】:

我想将 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") 的小提琴图,但我喜欢它们,半箱线图和半小提琴图,如图所示

感谢您的帮助

【问题讨论】:

    标签: python seaborn boxplot violin-plot


    【解决方案1】:

    这样做真的有兴趣吗?小提琴图已经在其中心包含了一个小箱线图。

    不过,这可以通过使用假色调级别并在两个图表之间切换顺序来实现:

    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
    

    【讨论】:

    • 完美,正是我所需要的
    【解决方案2】:

    您没有像您展示的图像那样具有三个维度。我相信您只是想要:

    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 以获得进一步的定制和文档

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-10
      • 1970-01-01
      • 1970-01-01
      • 2017-07-15
      相关资源
      最近更新 更多