Skip to content

Commit 70a36e2

Browse files
authored
Merge pull request matplotlib#28074 from saranti/boxplot_vert
Add `orientation` parameter to Boxplot and deprecate `vert`
2 parents 6e8fcbb + 1e312d0 commit 70a36e2

File tree

12 files changed

+679
-565
lines changed

12 files changed

+679
-565
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
``boxplot`` and ``bxp`` *vert* parameter, and ``rcParams["boxplot.vertical"]``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
The parameter *vert: bool* has been deprecated on `~.Axes.boxplot` and
5+
`~.Axes.bxp`. It is replaced by *orientation: {"vertical", "horizontal"}*
6+
for API consistency.
7+
8+
``rcParams["boxplot.vertical"]``, which controlled the orientation of ``boxplot``,
9+
is deprecated without replacement.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
``boxplot`` and ``bxp`` orientation parameter
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
Boxplots have a new parameter *orientation: {"vertical", "horizontal"}*
5+
to change the orientation of the plot. This replaces the deprecated
6+
*vert: bool* parameter.
7+
8+
9+
.. plot::
10+
:include-source: true
11+
:alt: Example of creating 4 horizontal boxplots.
12+
13+
import matplotlib.pyplot as plt
14+
import numpy as np
15+
16+
fig, ax = plt.subplots()
17+
np.random.seed(19680801)
18+
all_data = [np.random.normal(0, std, 100) for std in range(6, 10)]
19+
20+
ax.boxplot(all_data, orientation='horizontal')
21+
plt.show()

galleries/examples/statistics/boxplot_demo.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@
4646
axs[1, 0].set_title("don't show\noutlier points")
4747

4848
# horizontal boxes
49-
axs[1, 1].boxplot(data, sym='rs', vert=False)
49+
axs[1, 1].boxplot(data, sym='rs', orientation='horizontal')
5050
axs[1, 1].set_title('horizontal boxes')
5151

5252
# change whisker length
53-
axs[1, 2].boxplot(data, sym='rs', vert=False, whis=0.75)
53+
axs[1, 2].boxplot(data, sym='rs', orientation='horizontal', whis=0.75)
5454
axs[1, 2].set_title('change whisker length')
5555

5656
fig.subplots_adjust(left=0.08, right=0.98, bottom=0.05, top=0.9,
@@ -107,7 +107,7 @@
107107
fig.canvas.manager.set_window_title('A Boxplot Example')
108108
fig.subplots_adjust(left=0.075, right=0.95, top=0.9, bottom=0.25)
109109

110-
bp = ax1.boxplot(data, notch=False, sym='+', vert=True, whis=1.5)
110+
bp = ax1.boxplot(data, notch=False, sym='+', orientation='vertical', whis=1.5)
111111
plt.setp(bp['boxes'], color='black')
112112
plt.setp(bp['whiskers'], color='black')
113113
plt.setp(bp['fliers'], color='red', marker='+')

lib/matplotlib/axes/_axes.py

+62-17
Original file line numberDiff line numberDiff line change
@@ -3819,9 +3819,10 @@ def apply_mask(arrays, mask):
38193819
@_api.make_keyword_only("3.9", "notch")
38203820
@_preprocess_data()
38213821
@_api.rename_parameter("3.9", "labels", "tick_labels")
3822-
def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
3823-
positions=None, widths=None, patch_artist=None,
3824-
bootstrap=None, usermedians=None, conf_intervals=None,
3822+
def boxplot(self, x, notch=None, sym=None, vert=None,
3823+
orientation='vertical', whis=None, positions=None,
3824+
widths=None, patch_artist=None, bootstrap=None,
3825+
usermedians=None, conf_intervals=None,
38253826
meanline=None, showmeans=None, showcaps=None,
38263827
showbox=None, showfliers=None, boxprops=None,
38273828
tick_labels=None, flierprops=None, medianprops=None,
@@ -3877,9 +3878,21 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
38773878
the fliers. If `None`, then the fliers default to 'b+'. More
38783879
control is provided by the *flierprops* parameter.
38793880
3880-
vert : bool, default: :rc:`boxplot.vertical`
3881-
If `True`, draws vertical boxes.
3882-
If `False`, draw horizontal boxes.
3881+
vert : bool, optional
3882+
.. deprecated:: 3.10
3883+
Use *orientation* instead.
3884+
3885+
If this is given during the deprecation period, it overrides
3886+
the *orientation* parameter.
3887+
3888+
If True, plots the boxes vertically.
3889+
If False, plots the boxes horizontally.
3890+
3891+
orientation : {'vertical', 'horizontal'}, default: 'vertical'
3892+
If 'horizontal', plots the boxes horizontally.
3893+
Otherwise, plots the boxes vertically.
3894+
3895+
.. versionadded:: 3.10
38833896
38843897
whis : float or (float, float), default: 1.5
38853898
The position of the whiskers.
@@ -4047,8 +4060,6 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
40474060
labels=tick_labels, autorange=autorange)
40484061
if notch is None:
40494062
notch = mpl.rcParams['boxplot.notch']
4050-
if vert is None:
4051-
vert = mpl.rcParams['boxplot.vertical']
40524063
if patch_artist is None:
40534064
patch_artist = mpl.rcParams['boxplot.patchartist']
40544065
if meanline is None:
@@ -4148,13 +4159,14 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
41484159
meanline=meanline, showfliers=showfliers,
41494160
capprops=capprops, whiskerprops=whiskerprops,
41504161
manage_ticks=manage_ticks, zorder=zorder,
4151-
capwidths=capwidths, label=label)
4162+
capwidths=capwidths, label=label,
4163+
orientation=orientation)
41524164
return artists
41534165

41544166
@_api.make_keyword_only("3.9", "widths")
4155-
def bxp(self, bxpstats, positions=None, widths=None, vert=True,
4156-
patch_artist=False, shownotches=False, showmeans=False,
4157-
showcaps=True, showbox=True, showfliers=True,
4167+
def bxp(self, bxpstats, positions=None, widths=None, vert=None,
4168+
orientation='vertical', patch_artist=False, shownotches=False,
4169+
showmeans=False, showcaps=True, showbox=True, showfliers=True,
41584170
boxprops=None, whiskerprops=None, flierprops=None,
41594171
medianprops=None, capprops=None, meanprops=None,
41604172
meanline=False, manage_ticks=True, zorder=None,
@@ -4213,9 +4225,21 @@ def bxp(self, bxpstats, positions=None, widths=None, vert=True,
42134225
Either a scalar or a vector and sets the width of each cap.
42144226
The default is ``0.5*(width of the box)``, see *widths*.
42154227
4216-
vert : bool, default: True
4217-
If `True` (default), makes the boxes vertical.
4218-
If `False`, makes horizontal boxes.
4228+
vert : bool, optional
4229+
.. deprecated:: 3.10
4230+
Use *orientation* instead.
4231+
4232+
If this is given during the deprecation period, it overrides
4233+
the *orientation* parameter.
4234+
4235+
If True, plots the boxes vertically.
4236+
If False, plots the boxes horizontally.
4237+
4238+
orientation : {'vertical', 'horizontal'}, default: 'vertical'
4239+
If 'horizontal', plots the boxes horizontally.
4240+
Otherwise, plots the boxes vertically.
4241+
4242+
.. versionadded:: 3.10
42194243
42204244
patch_artist : bool, default: False
42214245
If `False` produces boxes with the `.Line2D` artist.
@@ -4334,8 +4358,29 @@ def merge_kw_rc(subkey, explicit, zdelta=0, usemarker=True):
43344358
if meanprops is None or removed_prop not in meanprops:
43354359
mean_kw[removed_prop] = ''
43364360

4361+
# vert and orientation parameters are linked until vert's
4362+
# deprecation period expires. vert only takes precedence
4363+
# if set to False.
4364+
if vert is None:
4365+
vert = mpl.rcParams['boxplot.vertical']
4366+
else:
4367+
_api.warn_deprecated(
4368+
"3.10",
4369+
name="vert: bool",
4370+
alternative="orientation: {'vertical', 'horizontal'}"
4371+
)
4372+
if vert is False:
4373+
orientation = 'horizontal'
4374+
_api.check_in_list(['horizontal', 'vertical'], orientation=orientation)
4375+
4376+
if not mpl.rcParams['boxplot.vertical']:
4377+
_api.warn_deprecated(
4378+
"3.10",
4379+
name='boxplot.vertical', obj_type="rcparam"
4380+
)
4381+
43374382
# vertical or horizontal plot?
4338-
maybe_swap = slice(None) if vert else slice(None, None, -1)
4383+
maybe_swap = slice(None) if orientation == 'vertical' else slice(None, None, -1)
43394384

43404385
def do_plot(xs, ys, **kwargs):
43414386
return self.plot(*[xs, ys][maybe_swap], **kwargs)[0]
@@ -4460,7 +4505,7 @@ def do_patch(xs, ys, **kwargs):
44604505
artist.set_label(lbl)
44614506

44624507
if manage_ticks:
4463-
axis_name = "x" if vert else "y"
4508+
axis_name = "x" if orientation == 'vertical' else "y"
44644509
interval = getattr(self.dataLim, f"interval{axis_name}")
44654510
axis = self._axis_map[axis_name]
44664511
positions = axis.convert_units(positions)

lib/matplotlib/axes/_axes.pyi

+3-1
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ class Axes(_AxesBase):
350350
notch: bool | None = ...,
351351
sym: str | None = ...,
352352
vert: bool | None = ...,
353+
orientation: Literal["vertical", "horizontal"] = ...,
353354
whis: float | tuple[float, float] | None = ...,
354355
positions: ArrayLike | None = ...,
355356
widths: float | ArrayLike | None = ...,
@@ -382,7 +383,8 @@ class Axes(_AxesBase):
382383
positions: ArrayLike | None = ...,
383384
*,
384385
widths: float | ArrayLike | None = ...,
385-
vert: bool = ...,
386+
vert: bool | None = ...,
387+
orientation: Literal["vertical", "horizontal"] = ...,
386388
patch_artist: bool = ...,
387389
shownotches: bool = ...,
388390
showmeans: bool = ...,

lib/matplotlib/mpl-data/stylelib/classic.mplstyle

-1
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,6 @@ boxplot.showbox: True
380380
boxplot.showcaps: True
381381
boxplot.showfliers: True
382382
boxplot.showmeans: False
383-
boxplot.vertical: True
384383
boxplot.whiskerprops.color: b
385384
boxplot.whiskerprops.linestyle: --
386385
boxplot.whiskerprops.linewidth: 1.0

lib/matplotlib/pyplot.py

+2
Original file line numberDiff line numberDiff line change
@@ -2935,6 +2935,7 @@ def boxplot(
29352935
notch: bool | None = None,
29362936
sym: str | None = None,
29372937
vert: bool | None = None,
2938+
orientation: Literal["vertical", "horizontal"] = "vertical",
29382939
whis: float | tuple[float, float] | None = None,
29392940
positions: ArrayLike | None = None,
29402941
widths: float | ArrayLike | None = None,
@@ -2967,6 +2968,7 @@ def boxplot(
29672968
notch=notch,
29682969
sym=sym,
29692970
vert=vert,
2971+
orientation=orientation,
29702972
whis=whis,
29712973
positions=positions,
29722974
widths=widths,
Binary file not shown.
Loading

0 commit comments

Comments
 (0)