Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7266b4f

Browse files
committed
Merge pull request #6153 from jenshnielsen/backport_5343
Backport #5343 from phobson/bxp-equal-quartiles
2 parents cd88b31 + 54ec43f commit 7266b4f

File tree

9 files changed

+214
-552
lines changed

9 files changed

+214
-552
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Changed default ``autorange`` behavior in boxplots
2+
``````````````````````````````````````````````````
3+
4+
Prior to v1.5.2, the whiskers of boxplots would extend to the mininum
5+
and maximum values if the quartiles were all equal (i.e., Q1 = median
6+
= Q3). This behavior has been disabled by default to restore consistency
7+
with other plotting packages.
8+
9+
To restore the old behavior, simply set ``autorange=True`` when
10+
calling ``plt.boxplot``.

lib/matplotlib/axes/_axes.py

Lines changed: 139 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -3043,9 +3043,10 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
30433043
positions=None, widths=None, patch_artist=None,
30443044
bootstrap=None, usermedians=None, conf_intervals=None,
30453045
meanline=None, showmeans=None, showcaps=None,
3046-
showbox=None, showfliers=None, boxprops=None, labels=None,
3047-
flierprops=None, medianprops=None, meanprops=None,
3048-
capprops=None, whiskerprops=None, manage_xticks=True):
3046+
showbox=None, showfliers=None, boxprops=None,
3047+
labels=None, flierprops=None, medianprops=None,
3048+
meanprops=None, capprops=None, whiskerprops=None,
3049+
manage_xticks=True, autorange=False):
30493050
"""
30503051
Make a box and whisker plot.
30513052
@@ -3055,161 +3056,167 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
30553056
positions=None, widths=None, patch_artist=False,
30563057
bootstrap=None, usermedians=None, conf_intervals=None,
30573058
meanline=False, showmeans=False, showcaps=True,
3058-
showbox=True, showfliers=True, boxprops=None, labels=None,
3059-
flierprops=None, medianprops=None, meanprops=None,
3060-
capprops=None, whiskerprops=None, manage_xticks=True):
3059+
showbox=True, showfliers=True, boxprops=None,
3060+
labels=None, flierprops=None, medianprops=None,
3061+
meanprops=None, capprops=None, whiskerprops=None,
3062+
manage_xticks=True, autorange=False):
30613063
3062-
Make a box and whisker plot for each column of *x* or each
3063-
vector in sequence *x*. The box extends from the lower to
3064+
Make a box and whisker plot for each column of ``x`` or each
3065+
vector in sequence ``x``. The box extends from the lower to
30643066
upper quartile values of the data, with a line at the median.
30653067
The whiskers extend from the box to show the range of the
30663068
data. Flier points are those past the end of the whiskers.
30673069
30683070
Parameters
30693071
----------
30703072
x : Array or a sequence of vectors.
3071-
The input data.
3072-
3073-
notch : bool, default = False
3074-
If False, produces a rectangular box plot.
3075-
If True, will produce a notched box plot
3076-
3077-
sym : str or None, default = None
3078-
The default symbol for flier points.
3079-
Enter an empty string ('') if you don't want to show fliers.
3080-
If `None`, then the fliers default to 'b+' If you want more
3081-
control use the flierprops kwarg.
3082-
3083-
vert : bool, default = True
3084-
If True (default), makes the boxes vertical.
3085-
If False, makes horizontal boxes.
3086-
3087-
whis : float, sequence (default = 1.5) or string
3088-
As a float, determines the reach of the whiskers past the first
3089-
and third quartiles (e.g., Q3 + whis*IQR, IQR = interquartile
3090-
range, Q3-Q1). Beyond the whiskers, data are considered outliers
3091-
and are plotted as individual points. Set this to an unreasonably
3092-
high value to force the whiskers to show the min and max values.
3093-
Alternatively, set this to an ascending sequence of percentile
3094-
(e.g., [5, 95]) to set the whiskers at specific percentiles of
3095-
the data. Finally, *whis* can be the string 'range' to force the
3096-
whiskers to the min and max of the data. In the edge case that
3097-
the 25th and 75th percentiles are equivalent, *whis* will be
3098-
automatically set to 'range'.
3099-
3100-
bootstrap : None (default) or integer
3101-
Specifies whether to bootstrap the confidence intervals
3102-
around the median for notched boxplots. If bootstrap==None,
3103-
no bootstrapping is performed, and notches are calculated
3104-
using a Gaussian-based asymptotic approximation (see McGill, R.,
3105-
Tukey, J.W., and Larsen, W.A., 1978, and Kendall and Stuart,
3106-
1967). Otherwise, bootstrap specifies the number of times to
3107-
bootstrap the median to determine it's 95% confidence intervals.
3108-
Values between 1000 and 10000 are recommended.
3109-
3110-
usermedians : array-like or None (default)
3111-
An array or sequence whose first dimension (or length) is
3112-
compatible with *x*. This overrides the medians computed by
3113-
matplotlib for each element of *usermedians* that is not None.
3114-
When an element of *usermedians* == None, the median will be
3115-
computed by matplotlib as normal.
3116-
3117-
conf_intervals : array-like or None (default)
3118-
Array or sequence whose first dimension (or length) is compatible
3119-
with *x* and whose second dimension is 2. When the current element
3120-
of *conf_intervals* is not None, the notch locations computed by
3121-
matplotlib are overridden (assuming notch is True). When an
3122-
element of *conf_intervals* is None, boxplot compute notches the
3123-
method specified by the other kwargs (e.g., *bootstrap*).
3124-
3125-
positions : array-like, default = [1, 2, ..., n]
3126-
Sets the positions of the boxes. The ticks and limits
3127-
are automatically set to match the positions.
3128-
3129-
widths : array-like, default = 0.5
3130-
Either a scalar or a vector and sets the width of each box. The
3131-
default is 0.5, or ``0.15*(distance between extreme positions)``
3132-
if that is smaller.
3133-
3134-
labels : sequence or None (default)
3135-
Labels for each dataset. Length must be compatible with
3136-
dimensions of *x*
3137-
3138-
patch_artist : bool, default = False
3139-
If False produces boxes with the Line2D artist
3140-
If True produces boxes with the Patch artist
3141-
3142-
showmeans : bool, default = False
3143-
If True, will toggle on the rendering of the means
3144-
3145-
showcaps : bool, default = True
3146-
If True, will toggle on the rendering of the caps
3147-
3148-
showbox : bool, default = True
3149-
If True, will toggle on the rendering of the box
3150-
3151-
showfliers : bool, default = True
3152-
If True, will toggle on the rendering of the fliers
3153-
3154-
boxprops : dict or None (default)
3155-
If provided, will set the plotting style of the boxes
3156-
3157-
whiskerprops : dict or None (default)
3158-
If provided, will set the plotting style of the whiskers
3073+
The input data.
3074+
3075+
notch : bool, optional (False)
3076+
If `True`, will produce a notched box plot. Otherwise, a
3077+
rectangular boxplot is produced.
3078+
3079+
sym : str, optional
3080+
The default symbol for flier points. Enter an empty string
3081+
('') if you don't want to show fliers. If `None`, then the
3082+
fliers default to 'b+' If you want more control use the
3083+
flierprops kwarg.
3084+
3085+
vert : bool, optional (True)
3086+
If `True` (default), makes the boxes vertical. If `False`,
3087+
everything is drawn horizontally.
3088+
3089+
whis : float, sequence, or string (default = 1.5)
3090+
As a float, determines the reach of the whiskers past the
3091+
first and third quartiles (e.g., Q3 + whis*IQR,
3092+
IQR = interquartile range, Q3-Q1). Beyond the whiskers, data
3093+
are considered outliers and are plotted as individual
3094+
points. Set this to an unreasonably high value to force the
3095+
whiskers to show the min and max values. Alternatively, set
3096+
this to an ascending sequence of percentile (e.g., [5, 95])
3097+
to set the whiskers at specific percentiles of the data.
3098+
Finally, ``whis`` can be the string ``'range'`` to force the
3099+
whiskers to the min and max of the data.
3100+
3101+
bootstrap : int, optional
3102+
Specifies whether to bootstrap the confidence intervals
3103+
around the median for notched boxplots. If `bootstrap` is None,
3104+
no bootstrapping is performed, and notches are calculated
3105+
using a Gaussian-based asymptotic approximation (see McGill,
3106+
R., Tukey, J.W., and Larsen, W.A., 1978, and Kendall and
3107+
Stuart, 1967). Otherwise, bootstrap specifies the number of
3108+
times to bootstrap the median to determine its 95%
3109+
confidence intervals. Values between 1000 and 10000 are
3110+
recommended.
3111+
3112+
usermedians : array-like, optional
3113+
An array or sequence whose first dimension (or length) is
3114+
compatible with ``x``. This overrides the medians computed
3115+
by matplotlib for each element of ``usermedians`` that is not
3116+
`None`. When an element of ``usermedians`` is None, the median
3117+
will be computed by matplotlib as normal.
3118+
3119+
conf_intervals : array-like, optional
3120+
Array or sequence whose first dimension (or length) is
3121+
compatible with ``x`` and whose second dimension is 2. When
3122+
the an element of ``conf_intervals`` is not None, the
3123+
notch locations computed by matplotlib are overridden
3124+
(provided ``notch`` is `True`). When an element of
3125+
``conf_intervals`` is `None`, the notches are computed by the
3126+
method specified by the other kwargs (e.g., ``bootstrap``).
3127+
3128+
positions : array-like, optional
3129+
Sets the positions of the boxes. The ticks and limits are
3130+
automatically set to match the positions. Defaults to
3131+
`range(1, N+1)` where N is the number of boxes to be drawn.
3132+
3133+
widths : scalar or array-like
3134+
Sets the width of each box either with a scalar or a
3135+
sequence. The default is 0.5, or ``0.15*(distance between
3136+
extreme positions)``, if that is smaller.
3137+
3138+
patch_artist : bool, optional (False)
3139+
If `False` produces boxes with the Line2D artist. Otherwise,
3140+
boxes and drawn with Patch artists.
3141+
3142+
labels : sequence, optional
3143+
Labels for each dataset. Length must be compatible with
3144+
dimensions of ``x``.
3145+
3146+
manage_xticks : bool, optional (True)
3147+
If the function should adjust the xlim and xtick locations.
31593148
3160-
capprops : dict or None (default)
3161-
If provided, will set the plotting style of the caps
3149+
autorange : bool, optional (False)
3150+
When `True` and the data are distributed such that the 25th and
3151+
75th percentiles are equal, ``whis`` is set to ``'range'`` such
3152+
that the whisker ends are at the minimum and maximum of the
3153+
data.
3154+
3155+
meanline : bool, optional (False)
3156+
If `True` (and ``showmeans`` is `True`), will try to render
3157+
the mean as a line spanning the full width of the box
3158+
according to ``meanprops`` (see below). Not recommended if
3159+
``shownotches`` is also True. Otherwise, means will be shown
3160+
as points.
3161+
3162+
Additional Options
3163+
---------------------
3164+
The following boolean options toggle the drawing of individual
3165+
components of the boxplots:
3166+
- showcaps: the caps on the ends of whiskers
3167+
(default is True)
3168+
- showbox: the central box (default is True)
3169+
- showfliers: the outliers beyond the caps (default is True)
3170+
- showmeans: the arithmetic means (default is False)
3171+
3172+
The remaining options can accept dictionaries that specify the
3173+
style of the individual artists:
3174+
- capprops
3175+
- boxprops
3176+
- whiskerprops
3177+
- flierprops
3178+
- medianprops
3179+
- meanprops
31623180
3163-
flierprops : dict or None (default)
3164-
If provided, will set the plotting style of the fliers
3181+
Returns
3182+
-------
3183+
result : dict
3184+
A dictionary mapping each component of the boxplot to a list
3185+
of the :class:`matplotlib.lines.Line2D` instances
3186+
created. That dictionary has the following keys (assuming
3187+
vertical boxplots):
31653188
3166-
medianprops : dict or None (default)
3167-
If provided, will set the plotting style of the medians
3189+
- ``boxes``: the main body of the boxplot showing the
3190+
quartiles and the median's confidence intervals if
3191+
enabled.
31683192
3169-
meanprops : dict or None (default)
3170-
If provided, will set the plotting style of the means
3193+
- ``medians``: horizontal lines at the median of each box.
31713194
3172-
meanline : bool, default = False
3173-
If True (and *showmeans* is True), will try to render the mean
3174-
as a line spanning the full width of the box according to
3175-
*meanprops*. Not recommended if *shownotches* is also True.
3176-
Otherwise, means will be shown as points.
3195+
- ``whiskers``: the vertical lines extending to the most
3196+
extreme, non-outlier data points.
31773197
3178-
manage_xticks : bool, default = True
3179-
If the function should adjust the xlim and xtick locations.
3198+
- ``caps``: the horizontal lines at the ends of the
3199+
whiskers.
31803200
3181-
Returns
3182-
-------
3201+
- ``fliers``: points representing data that extend beyond
3202+
the whiskers (fliers).
31833203
3184-
result : dict
3185-
A dictionary mapping each component of the boxplot
3186-
to a list of the :class:`matplotlib.lines.Line2D`
3187-
instances created. That dictionary has the following keys
3188-
(assuming vertical boxplots):
3189-
3190-
- boxes: the main body of the boxplot showing the quartiles
3191-
and the median's confidence intervals if enabled.
3192-
- medians: horizonal lines at the median of each box.
3193-
- whiskers: the vertical lines extending to the most extreme,
3194-
n-outlier data points.
3195-
- caps: the horizontal lines at the ends of the whiskers.
3196-
- fliers: points representing data that extend beyond the
3197-
whiskers (outliers).
3198-
- means: points or lines representing the means.
3204+
- ``means``: points or lines representing the means.
31993205
32003206
Examples
32013207
--------
3202-
32033208
.. plot:: mpl_examples/statistics/boxplot_demo.py
3209+
32043210
"""
3211+
32053212
# If defined in matplotlibrc, apply the value from rc file
32063213
# Overridden if argument is passed
32073214
if whis is None:
32083215
whis = rcParams['boxplot.whiskers']
32093216
if bootstrap is None:
32103217
bootstrap = rcParams['boxplot.bootstrap']
32113218
bxpstats = cbook.boxplot_stats(x, whis=whis, bootstrap=bootstrap,
3212-
labels=labels)
3219+
labels=labels, autorange=autorange)
32133220
if notch is None:
32143221
notch = rcParams['boxplot.notch']
32153222
if vert is None:
@@ -3455,10 +3462,10 @@ def bxp(self, bxpstats, positions=None, widths=None, vert=True,
34553462
quartiles and the median's confidence intervals if
34563463
enabled.
34573464
3458-
- ``medians``: horizonal lines at the median of each box.
3465+
- ``medians``: horizontal lines at the median of each box.
34593466
34603467
- ``whiskers``: the vertical lines extending to the most
3461-
extreme, n-outlier data points.
3468+
extreme, non-outlier data points.
34623469
34633470
- ``caps``: the horizontal lines at the ends of the
34643471
whiskers.

0 commit comments

Comments
 (0)