【问题标题】:Seaborn Plot including different distributions of the same dataSeaborn Plot 包括相同数据的不同分布
【发布时间】:2016-10-18 01:53:35
【问题描述】:

我希望创建一个seabornpointplot 以在一列中显示完整的数据分布,以及最低 25% 值的分布和最高 25% 值的分布,并且全部并排侧(在 x 轴上)。 到目前为止,我的尝试为我提供了这些值,但它们仅显示在 x 轴的同一部分,而不是在图表上从左到右展开,并且没有明显的方法来标记 x 刻度中的点(我更喜欢,而不是通过传说)。

import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib notebook

df = sns.load_dataset('tips')
df1 = df[(df.total_bill < df.total_bill.quantile(.25))]
df2 = df[(df.total_bill > df.total_bill.quantile(.75))]

sns.pointplot(y=df['total_bill'], data=df, color='red')
sns.pointplot(y=df1['total_bill'], data=df1, color='green')
sns.pointplot(y=df2['total_bill'], data=df2, color='blue')

【问题讨论】:

    标签: python pandas matplotlib machine-learning seaborn


    【解决方案1】:

    您可以 .join() 将新发行版添加到现有的 df,然后使用宽格式 .plot()

    lower, upper = df.total_bill.quantile([.25, .75]).values.tolist()
    df = df.join(df.loc[df.total_bill < lower, 'total_bill'], rsuffix='_lower')
    df = df.join(df.loc[df.total_bill > upper, 'total_bill'], rsuffix='_upper')
    sns.pointplot(data=df.loc[:, [c for c in df.columns if c.startswith('total')]])
    

    得到:

    如果你想添加组,你可以简单地使用.unstack() 来获得long 格式:

    df = df.loc[:, ['total_bill', 'total_bill_upper', 'total_bill_lower']].unstack().reset_index().drop('level_1', axis=1).dropna()
    df.columns = ['grp', 'val']
    

    得到:

    sns.pointplot(x='grp', y='val', hue='grp', data=df)
    

    【讨论】:

      【解决方案2】:

      我会考虑添加一个“组”,然后绘制为单个 DataFrame。

      import seaborn as sns
      import pandas as pd
      import matplotlib.pyplot as plt
      %matplotlib notebook
      
      df = sns.load_dataset('tips')
      df = df.append(df)
      
      df.loc[(df.total_bill < df.total_bill.quantile(.25)),'group'] = 'L'
      df.loc[(df.total_bill > df.total_bill.quantile(.75)),'group'] = 'H'
      df = df.reset_index(drop=True)
      df.loc[len(df)/2:,'group'] = 'all'
      
      sns.pointplot(data = df,
                    y='total_bill',
                    x='group',
                    hue='group',
                    linestyles='')
      

      【讨论】:

        猜你喜欢
        • 2020-09-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多