获取箱线图的数据- pandas [英] Getting Data of a 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()
这篇关于获取箱线图的数据- pandas 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!