使用python和matplotlib获取boxplot中使用的值 [英] Obtaining values used in boxplot, using python and matplotlib

查看:151
本文介绍了使用python和matplotlib获取boxplot中使用的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以从数据中绘制箱形图:

I can draw a boxplot from data:

import numpy as np
import matplotlib.pyplot as plt

data = np.random.rand(100)
plt.boxplot(data)

然后,该框的范围从25%到75%,而晶须的范围从最小值到最大值(25%-1.5 * IQR,75%+ 1.5 * IQR) ,其中IQR表示四分位间距. (当然,值1.5是可自定义的.)

Then, the box will range from the 25th-percentile to 75th-percentile, and the whisker will range from the smallest value to the largest value between (25th-percentile - 1.5*IQR, 75th-percentile + 1.5*IQR), where the IQR denotes the inter-quartile range. (Of course, the value 1.5 is customizable).

现在我想知道箱线图中使用的值,即中值,上下四分位数,上晶须终点和下晶须终点.虽然使用np.median()和np.percentile()很容易获得前三个,但晶须的终点将需要一些冗长的编码:

Now I want to know the values used in the boxplot, i.e. the median, upper and lower quartile, the upper whisker end point and the lower whisker end point. While the former three is easy to obtain by using np.median() and np.percentile(), the end point of the whiskers will require some verbose coding:

median = np.median(data)
upper_quartile = np.percentile(data, 75)
lower_quartile = np.percentile(data, 25)

iqr = upper_quartile - lower_quartile
upper_whisker = data[data<=upper_quartile+1.5*iqr].max()
lower_whisker = data[data>=lower_quartile-1.5*iqr].min()

我想知道,虽然这是可以接受的,但是会有更整洁的方法吗?似乎应该已经准备好从箱图中提取出这些值,因为它已经绘制了.

I was wondering, while this is acceptable, would there be a neater way to do this? It seems that the values should be ready to pull-out from the boxplot, as it's already drawn.

谢谢!

推荐答案

为什么要这么做?您正在做的事情已经很直接了.

Why do you want to do so? what you are doing is already pretty direct.

是的,如果要为绘图获取它们,则在完成绘图后,只需使用get_ydata()方法.

Yeah, if you want to fetch them for the plot, when the plot is already made, simply use the get_ydata() method.

B = plt.boxplot(data)
[item.get_ydata() for item in B['whiskers']]

它为每个晶须返回一个形状为(2,)的数组,第二个元素是我们想要的值:

It returns an array of the shape (2,) for each whiskers, the second element is the value we want:

[item.get_ydata()[1] for item in B['whiskers']]

这篇关于使用python和matplotlib获取boxplot中使用的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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