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

Skip to content

Commit 07b29f7

Browse files
committed
add boxplot orientation param
1 parent 9955a7c commit 07b29f7

File tree

11 files changed

+664
-562
lines changed

11 files changed

+664
-562
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
``boxplot`` and ``bxp`` *vert* parameter
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
The parameter *vert: bool* has been deprecated on `~.Axes.boxplot` and
5+
`~.Axes.bxp`.
6+
It is replaced by *orientation: {"vertical", "horizontal"}* for API
7+
consistency.
Lines changed: 21 additions & 0 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 51 additions & 15 deletions
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,
@@ -3878,8 +3879,20 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
38783879
control is provided by the *flierprops* parameter.
38793880
38803881
vert : bool, default: :rc:`boxplot.vertical`
3881-
If `True`, draws vertical boxes.
3882-
If `False`, draw horizontal boxes.
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,
@@ -4214,8 +4226,20 @@ def bxp(self, bxpstats, positions=None, widths=None, vert=True,
42144226
The default is ``0.5*(width of the box)``, see *widths*.
42154227
42164228
vert : bool, default: True
4217-
If `True` (default), makes the boxes vertical.
4218-
If `False`, makes horizontal boxes.
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,20 @@ 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. If both are selected,
4363+
# vert takes precedence.
4364+
if vert is not None:
4365+
_api.warn_deprecated(
4366+
"3.10",
4367+
name="vert: bool",
4368+
alternative="orientation: {'vertical', 'horizontal'}"
4369+
)
4370+
orientation = 'vertical' if vert else 'horizontal'
4371+
_api.check_in_list(['horizontal', 'vertical'], orientation=orientation)
4372+
43374373
# vertical or horizontal plot?
4338-
maybe_swap = slice(None) if vert else slice(None, None, -1)
4374+
maybe_swap = slice(None) if orientation == 'vertical' else slice(None, None, -1)
43394375

43404376
def do_plot(xs, ys, **kwargs):
43414377
return self.plot(*[xs, ys][maybe_swap], **kwargs)[0]
@@ -4460,7 +4496,7 @@ def do_patch(xs, ys, **kwargs):
44604496
artist.set_label(lbl)
44614497

44624498
if manage_ticks:
4463-
axis_name = "x" if vert else "y"
4499+
axis_name = "x" if orientation == 'vertical' else "y"
44644500
interval = getattr(self.dataLim, f"interval{axis_name}")
44654501
axis = self._axis_map[axis_name]
44664502
positions = axis.convert_units(positions)

lib/matplotlib/axes/_axes.pyi

Lines changed: 3 additions & 1 deletion
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/pyplot.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2918,6 +2918,7 @@ def boxplot(
29182918
notch: bool | None = None,
29192919
sym: str | None = None,
29202920
vert: bool | None = None,
2921+
orientation: Literal["vertical", "horizontal"] = "vertical",
29212922
whis: float | tuple[float, float] | None = None,
29222923
positions: ArrayLike | None = None,
29232924
widths: float | ArrayLike | None = None,
@@ -2950,6 +2951,7 @@ def boxplot(
29502951
notch=notch,
29512952
sym=sym,
29522953
vert=vert,
2954+
orientation=orientation,
29532955
whis=whis,
29542956
positions=positions,
29552957
widths=widths,
Binary file not shown.

0 commit comments

Comments
 (0)