How to get boxplot data for matplotlib boxplots

后端 未结 2 1928
灰色年华
灰色年华 2021-02-05 16:48

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

2条回答
  •  广开言路
    2021-02-05 17:25

    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"]]
    

    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])
    

    or, as suggested by WoodChopper

    stats = df.describe()
    

提交回复
热议问题