Matplotlib boxplot仅显示最大和最小传单 [英] Matplotlib boxplot show only max and min fliers

查看:169
本文介绍了Matplotlib boxplot仅显示最大和最小传单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用plt.boxplot()命令制作标准的Matplotlib箱线图。
创建箱形图的代码行是:

I am making standard Matplotlib boxplots using the plt.boxplot() command. My line of code that creates the boxplot is:

bp = plt.boxplot(data, whis=[5, 95], showfliers=True)

由于我的数据分布较大,我得到了很多晶须范围之外的传单。为了获得更清晰的出版物质量图,我只想显示最大单张传单。并且在最小。数据值,而不是所有传单。这可能吗?我没有在文档中看到任何内置选项可以做到这一点。

Because my data has a large distribution, I am getting a lot of fliers outside the range of the whiskers. To get a cleaner publication quality plot, I would like to only show single fliers at the max. and at the min. values of the data, instead of all fliers. Is this possible? I don't see any built-in options in the documentation to do this.

(我可以将晶须的范围设置为max / min,但这不是我想要的是,我想将晶须保持在第5个百分位数和第95个百分位数。

(I can set the range of the whiskers to max/min, but this is not what I want. I would like to keep the whiskers at the 5th and 95th percentiles).

下面是我正在研究的图形。注意传单的密度。

Below is the figure I am working on. Notice the density of fliers.

推荐答案

plt.boxplot()返回一个字典,其中的键 fliers 包含上限和下限飞行器作为line2d对象。您可以在进行如下绘制之前对其进行操作:

plt.boxplot() returns a dictionary, where the key fliers contains the upper and lower fliers as line2d objects. You can manipulate them before plotting like this:

仅在matplotlib> = 1.4.0

bp = plt.boxplot(data, whis=[5, 95], showfliers=True)

# Get a list of Line2D objects, representing a single line from the
# minimum to the maximum flier points.
fliers = bp['fliers']

# Iterate over it!
for fly in fliers:
    fdata = fly.get_data()
    fly.set_data([fdata[0][0],fdata[0][-1]],[fdata[1][0],fdata[1][-1]])

在较早的版本上版本

如果您使用的是matplotlib的较旧版本,则每个箱形图的传单均以两行表示,而不是一行。因此,循环看起来像这样:

If you are on an older version of matplotlib, the fliers for each boxplot are represented by two lines, not one. Thus, the loop would look something like this:

import numpy as np
for i in range(len(fliers)):
    fdata = fliers[i].get_data()
    # Get the index of the maximum y in data if 
    # i is 0 or even, else get index of minimum y.
    if i%2 == 0:
        id = np.where(fdata[1] == fdata[1].max())[0][0]
    else:
        id = np.where(fdata[1] == fdata[1].min())[0][0]
    fliers[i].set_data([fdata[0][id], fdata[1][id]])

还请注意, showfliers 参数在matplotlib< 1.4x中不存在,并且 whisk 参数不接受列表。

Also note that the showfliers argument doesn't exist in matplotlib <1.4x and the whisk argument doesn't accept lists.

当然(对于简单的应用程序),您可以绘制没有传单的箱形图,并将最大和最小点添加到图中:

Of course (for simple applications) you could plot the boxplot without fliers and add the max and min points to the plot:

bp = plt.boxplot(data, whis=[5, 95], showfliers=False)
sc = plt.scatter([1, 1], [data.min(), data.max()])

其中 [1,1] 是点的x位置。

这篇关于Matplotlib boxplot仅显示最大和最小传单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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