【问题标题】:seaborn violin plot for single column splitting by a categorical columnseaborn小提琴图,用于按分类列拆分单列
【发布时间】:2021-02-23 08:45:17
【问题描述】:

我有一个如下所示的数据框:

    num_column    is_train
    30.75               1
    12.05               1 
    ..                 ..
    43.79               0         
    15.35               0              

我想使用小提琴图查看num_column 的分布,小提琴的每一侧(或分割)在is_train 列中显示我的两个类别中的每一个的数据。

来自examples in documentation,这是我能想到的:

import seaborn as sns
sns.violinplot(x=merged_data.loc[:,'num_column'], hue=merged_data.loc[:,'is_train'], split=True)

从这个结果,我可以看到参数huesplit 根本没有效果。意思是小提琴的两边没有分开,我看不到任何传说,所以我假设hue 参数没有效果。

我正在尝试比较我的训练数据和测试数据中列的分布。

【问题讨论】:

    标签: python pandas matplotlib seaborn violin-plot


    【解决方案1】:

    您可以使用x= 参数来创建多个小提琴。当需要通过第三列进行区分时,使用huesplit 参数。

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    import seaborn as sns
    
    merged_data = pd.DataFrame({'num_column': 20 + np.random.randn(1000).cumsum(),
                                'is_train': np.repeat([0, 1], 500)})
    sns.violinplot(data=merged_data, x='is_train', y='num_column')
    plt.show()
    

    【讨论】:

      【解决方案2】:

      split= 参数将与hue-nesting 一起使用,只有当您已经有x= 参数时才能使用它。因此,您需要为x(两个数据集的值应该相同)和hue(根据数据集编码)提供列:

      merged_data['dummy'] = 0
      sns.violinplot(data=merged_data, y='num_column', split=True, hue='is_train', x='dummy')
      

      【讨论】:

        猜你喜欢
        • 2023-03-31
        • 2021-08-29
        • 2017-05-25
        • 2020-07-03
        • 2020-06-18
        • 2018-02-18
        • 2022-01-23
        • 2021-02-16
        • 2016-02-18
        相关资源
        最近更新 更多