如何获取Matplotlib BoxPlot的BoxPlot数据 [英] How to get boxplot data for matplotlib boxplots

查看:52
本文介绍了如何获取Matplotlib BoxPlot的BoxPlot数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取为在 Pandas 中绘制箱线图而生成的统计数据(使用数据框创建箱线图).即四分位数1,四分位数2,四分位数3,较低的晶须值,较高的晶须值和离群值.我尝试了以下查询来绘制箱线图.

I need to get the statistical data which were generated to draw a box plot in Pandas(using dataframe to create boxplots). i.e. Quartile1,Quartile2,Quartile3, lower whisker value, upper whisker value and outliers. I tried the following query to draw the boxplot.

import pandas as pd
df = pd.DataFrame(np.random.rand(100, 5), columns=['A', 'B', 'C', 'D', 'E'])
pd.DataFrame.boxplot(df,return_type = 'both')

有没有办法代替手动计算值?

Is there a way to do it instead of manually calculating the values?

推荐答案

一种选择是使用图中的 y 数据 - 可能对异常值(传单)最有用

One option is to use the y data from the plots - probably most useful for the outliers (fliers)

_, bp = pd.DataFrame.boxplot(df, return_type='both')

outliers = [flier.get_ydata() for flier in bp["fliers"]]
boxes = [box.get_ydata() for box in bp["boxes"]]
medians = [median.get_ydata() for median in bp["medians"]]
whiskers = [whiskers.get_ydata() for whiskers in bp["whiskers"]]

但使用任一方法获取其他值(包括 IQR)可能更直接

But it's probably more straightforward to get the other values (including IQR) using either

quantiles = df.quantile([0.01, 0.25, 0.5, 0.75, 0.99])

或者,按照 WoodChopper 的建议

or, as suggested by WoodChopper

stats = df.describe()

这篇关于如何获取Matplotlib BoxPlot的BoxPlot数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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