如何将百分位数的seaborn boxplot晶须作为基础? [英] How to base seaborn boxplot whiskers on percentiles?

查看:96
本文介绍了如何将百分位数的seaborn boxplot晶须作为基础?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用箱线图来显示组之间值分布的差异。较低(25)和较高(75)的百分位数以及中位数指示组之间的分布和主要差异。然而,晶须不太清楚。默认情况下,在matlibplot或seaborn中,箱线图的晶须表示内四分位数范围(IQR)的倍数(默认值:1.5),该值是内框所覆盖的值的范围。超出此范围的点将被识别为离群值。 seaborn和matlibplot都有相同的命令来更改晶须的位置:

I am using a boxplot to show differences in the distribution of values between groups. The lower(25) and higher (75) percentiles and the median are indicative of the distribution and main differences between groups. The whiskers are however less clear. By default in matlibplot or seaborn, the whiskers of a boxplot are a representation of a multiple (default: 1.5) of the innerquartile range (IQR), which is the range of values covered by the inner box. Points outside this range will be identified as outliers. Both seaborn and matlibplot have the same command to change the location of the whiskers:

whis : float, 
Proportion of the IQR past the low and high quartiles to extend the plot whiskers.Points outside this range will be identified as outliers.

例如:

boxplots = ax.boxplot(myData, whis=1.5)

或者,matlibplot还允许将晶须基于百分位数。这对于我想用数据讲述的故事更有效。例如:

Alternatively, matlibplot also allows to base the whiskers on percentiles. This works better for the story I am trying to tell with my data. For example:

boxplots = ax.boxplot(myData, whis=[5, 95])

与matlibplot相反, whis = [5,95] 并不在Seaborn工作。现在,我正在寻找基于百分位数定义Seaborn晶须的方法。

In contrast to matlibplot, the whis=[5, 95] does not work in Seaborn. Now I am looking for way to define the whiskers in Seaborn based on percentiles.

我的第一个想法是根据百分位数从matlibplot中获取晶须的值,并找到相应的比例IQR晶须值。这就是我所做的:

My first idea was to get the values of the whiskers from matlibplot based on percentiles and find the corresponding proportional IQR whisker value. This is what I did:

for w in np.arange(0.00,2.00, 0.01):    
        fig, ax = plt.subplots(ncols=2, nrows=1,figsize=(8, 6))
        bp = ax[0].boxplot(myData, whis=[5, 95])
        ax[0].set_xlabel('bp')
        ap = ax[1].boxplot(myData, whis=w)
        ax[1].set_xlabel('ap')

        r = 3

        alo =  (np.round(bp['whiskers'][0].get_ydata(), r))
        blo =  (np.round(ap['whiskers'][0].get_ydata(), r))
        ahi =  (np.round(bp['whiskers'][1].get_ydata(), r))
        bhi =  (np.round(ap['whiskers'][1].get_ydata(), r))

        plt.close()

        if [alo == blo] == [True, True]:
            if [ahi == bhi] == [True, True]:
                print w, "|", alo[1], "=", blo[1], '&', ahi[1], "=", bhi[1]

但是问题是是,这仅适用于我的数据不符合要求的完全正态分布。因此,我真的很想找到一种在Seaborn箱形图中将百分位数用于晶须的方法。

The problem however is that this only works for perfectly normal distribution which my data does not meet. So I would really like find a way to use percentiles for the whiskers in Seaborn boxplots. Is there any way to do this?

推荐答案

Seaborn在这方面似乎与matplotlib相同:

Seaborn seems to work the same as matplotlib in this regard:

tips = sns.load_dataset("tips")
ax = sns.boxplot(x=tips["total_bill"], whis=[5, 95])
plt.grid(True)

plt.boxplot(tips["total_bill"], whis=[5, 95], vert=False)
plt.grid(True)

我想seaborn只需将 whis 传递给matplotlib方法。该文档字符串可能是从matplotlib的早期版本复制的。

I guess seaborn just pass whis to the matplotlib method. The docstring might have been copied from an earlier version of matplotlib.

这篇关于如何将百分位数的seaborn boxplot晶须作为基础?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆