获取方框图的数据-Matplotlib



我必须绘制一些数据的箱形图,这可以很容易地使用Matplotlib来完成。然而,我被要求提供一张表格,里面有那里提供的数据,比如晶须、中值、标准偏差等等

我知道我可以"手工"计算这些,但从参考文献中我也知道boxplot方法:

Returns a dictionary mapping each component of the boxplot to a list of the matplotlib.lines.Line2D instances created. That dictionary has the following keys (assuming vertical boxplots):
boxes: the main body of the boxplot showing the quartiles and the median’s confidence intervals if enabled.
medians: horizonal lines at the median of each box.
whiskers: the vertical lines extending to the most extreme, n-outlier data points.
caps: the horizontal lines at the ends of the whiskers.
fliers: points representing data that extend beyone the whiskers (outliers).

所以我想知道如何获得这些值,因为它们是matplotlib.lines.Line2D.

谢谢。

正如您所了解的,您需要访问boxplot的返回值的成员。

也就是说,例如,如果您的返回值存储在bp

bp['medians'][0].get_ydata()
>> array([ 2.5,  2.5])

由于方框图是垂直的,因此中线是水平线,您只需要关注其中一个y值;即,我的样本数据的中值是2.5。

对于字典中的每个"键",值将是一个列表,用于处理多个框。如果您只有一个方框图,那么列表将只有一个元素,因此我使用了上面的bp['medians'][0]。如果你的方框图中有多个方框,你需要使用例如对它们进行迭代

for medline in bp['medians']:
    linedata = medline.get_ydata()
    median = linedata[0]

不幸的是,朱的回答没有奏效,因为不同的元素表现不同。例如,只有一个中间带,但有两个须。。。因此,按上述方法手动处理每个数量是最安全的。

注意:你最接近的是以下内容;

res  = {}
for key, value in bp.items():
    res[key] = [v.get_data() for v in value]

或等效

res = {key : [v.get_data() for v in value] for key, value in bp.items()}

最新更新