Skip to content

[Doc]: pyplot.boxplot whisker length wrong docs #25134

Closed
@RadostW

Description

@RadostW

Documentation Link

https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.boxplot.html

Problem

Documentation says that

The whiskers extend from the box by 1.5x the inter-quartile range (IQR).

This is clearly not true as in default configuration upper and lower whisker can have completely different length.

You can try the following code for a very visible example.

import matplotlib.pyplot as plt
plt.boxplot([-4.7 , -1.29, -5.78,  2.73, -5.54])

assymetric_whiskers

I managed to track the source of this behaviour to relevant lines in cbook (https://github.com/matplotlib/matplotlib/blob/c23ccdde6f0f8c071b09a88770e24452f2859e99/lib/matplotlib/cbook/__init__.py). I have attached relevant lines with my comments below

# 1.5 * IQR below Q1 location
loval = q1 - whis * stats['iqr']

# select points that are above loval
wisklo = x[x >= loval]

if len(wisklo) == 0 or np.min(wisklo) > q1:
    # there are no points above loval (pathological case)
    # OR
    # points above loval fit inside the box
    
    stats['whislo'] = q1
    # set whisker length to zero
    
else:
    # there are some points in the range [Q1 - 1.5 IQR, Q1]
    
    stats['whislo'] = np.min(wisklo)
    # put whiskers end exactly at the lowest point

Suggested improvement

I suggest making the relevant description more accurate. Replace current wording:

The box extends from the first quartile (Q1) to the third quartile (Q3) of the data, with a line at the median. The whiskers extend from the box by 1.5x the inter-quartile range (IQR). Flier points are those past the end of the whiskers

With a slightly longer but correct description:

The box extends from the first quartile (Q1) to the third quartile (Q3) of the data, with a line at the median.
The whiskers extend from the box to the farthest point within 1.5x the inter-quartile range (IQR) distance. Flier points are those past the end of the whiskers

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions