Skip to content

Commit

Permalink
add boxplot orientation param
Browse files Browse the repository at this point in the history
  • Loading branch information
saranti committed Apr 27, 2024
1 parent 9955a7c commit 07b29f7
Show file tree
Hide file tree
Showing 11 changed files with 664 additions and 562 deletions.
7 changes: 7 additions & 0 deletions doc/api/next_api_changes/deprecations/28074-TS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
``boxplot`` and ``bxp`` *vert* parameter
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The parameter *vert: bool* has been deprecated on `~.Axes.boxplot` and
`~.Axes.bxp`.
It is replaced by *orientation: {"vertical", "horizontal"}* for API
consistency.
21 changes: 21 additions & 0 deletions doc/users/next_whats_new/boxplot_orientation.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
``boxplot`` and ``bxp`` orientation parameter
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Boxplots have a new parameter *orientation: {"vertical", "horizontal"}*
to change the orientation of the plot. This replaces the deprecated
*vert: bool* parameter.


.. plot::
:include-source: true
:alt: Example of creating 4 horizontal boxplots.

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
np.random.seed(19680801)
all_data = [np.random.normal(0, std, 100) for std in range(6, 10)]

ax.boxplot(all_data, orientation='horizontal')
plt.show()
6 changes: 3 additions & 3 deletions galleries/examples/statistics/boxplot_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@
axs[1, 0].set_title("don't show\noutlier points")

# horizontal boxes
axs[1, 1].boxplot(data, sym='rs', vert=False)
axs[1, 1].boxplot(data, sym='rs', orientation='horizontal')
axs[1, 1].set_title('horizontal boxes')

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

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

bp = ax1.boxplot(data, notch=False, sym='+', vert=True, whis=1.5)
bp = ax1.boxplot(data, notch=False, sym='+', orientation='vertical', whis=1.5)
plt.setp(bp['boxes'], color='black')
plt.setp(bp['whiskers'], color='black')
plt.setp(bp['fliers'], color='red', marker='+')
Expand Down
66 changes: 51 additions & 15 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3819,9 +3819,10 @@ def apply_mask(arrays, mask):
@_api.make_keyword_only("3.9", "notch")
@_preprocess_data()
@_api.rename_parameter("3.9", "labels", "tick_labels")
def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
positions=None, widths=None, patch_artist=None,
bootstrap=None, usermedians=None, conf_intervals=None,
def boxplot(self, x, notch=None, sym=None, vert=None,
orientation='vertical', whis=None, positions=None,
widths=None, patch_artist=None, bootstrap=None,
usermedians=None, conf_intervals=None,
meanline=None, showmeans=None, showcaps=None,
showbox=None, showfliers=None, boxprops=None,
tick_labels=None, flierprops=None, medianprops=None,
Expand Down Expand Up @@ -3878,8 +3879,20 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
control is provided by the *flierprops* parameter.
vert : bool, default: :rc:`boxplot.vertical`
If `True`, draws vertical boxes.
If `False`, draw horizontal boxes.
.. deprecated:: 3.10
Use *orientation* instead.
If this is given during the deprecation period, it overrides
the *orientation* parameter.
If True, plots the boxes vertically.
If False, plots the boxes horizontally.
orientation : {'vertical', 'horizontal'}, default: 'vertical'
If 'horizontal', plots the boxes horizontally.
Otherwise, plots the boxes vertically.
.. versionadded:: 3.10
whis : float or (float, float), default: 1.5
The position of the whiskers.
Expand Down Expand Up @@ -4047,8 +4060,6 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
labels=tick_labels, autorange=autorange)
if notch is None:
notch = mpl.rcParams['boxplot.notch']
if vert is None:
vert = mpl.rcParams['boxplot.vertical']
if patch_artist is None:
patch_artist = mpl.rcParams['boxplot.patchartist']
if meanline is None:
Expand Down Expand Up @@ -4148,13 +4159,14 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
meanline=meanline, showfliers=showfliers,
capprops=capprops, whiskerprops=whiskerprops,
manage_ticks=manage_ticks, zorder=zorder,
capwidths=capwidths, label=label)
capwidths=capwidths, label=label,
orientation=orientation)
return artists

@_api.make_keyword_only("3.9", "widths")
def bxp(self, bxpstats, positions=None, widths=None, vert=True,
patch_artist=False, shownotches=False, showmeans=False,
showcaps=True, showbox=True, showfliers=True,
def bxp(self, bxpstats, positions=None, widths=None, vert=None,
orientation='vertical', patch_artist=False, shownotches=False,
showmeans=False, showcaps=True, showbox=True, showfliers=True,
boxprops=None, whiskerprops=None, flierprops=None,
medianprops=None, capprops=None, meanprops=None,
meanline=False, manage_ticks=True, zorder=None,
Expand Down Expand Up @@ -4214,8 +4226,20 @@ def bxp(self, bxpstats, positions=None, widths=None, vert=True,
The default is ``0.5*(width of the box)``, see *widths*.
vert : bool, default: True
If `True` (default), makes the boxes vertical.
If `False`, makes horizontal boxes.
.. deprecated:: 3.10
Use *orientation* instead.
If this is given during the deprecation period, it overrides
the *orientation* parameter.
If True, plots the boxes vertically.
If False, plots the boxes horizontally.
orientation : {'vertical', 'horizontal'}, default: 'vertical'
If 'horizontal', plots the boxes horizontally.
Otherwise, plots the boxes vertically.
.. versionadded:: 3.10
patch_artist : bool, default: False
If `False` produces boxes with the `.Line2D` artist.
Expand Down Expand Up @@ -4334,8 +4358,20 @@ def merge_kw_rc(subkey, explicit, zdelta=0, usemarker=True):
if meanprops is None or removed_prop not in meanprops:
mean_kw[removed_prop] = ''

# vert and orientation parameters are linked until vert's
# deprecation period expires. If both are selected,
# vert takes precedence.
if vert is not None:
_api.warn_deprecated(
"3.10",
name="vert: bool",
alternative="orientation: {'vertical', 'horizontal'}"
)
orientation = 'vertical' if vert else 'horizontal'
_api.check_in_list(['horizontal', 'vertical'], orientation=orientation)

# vertical or horizontal plot?
maybe_swap = slice(None) if vert else slice(None, None, -1)
maybe_swap = slice(None) if orientation == 'vertical' else slice(None, None, -1)

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

if manage_ticks:
axis_name = "x" if vert else "y"
axis_name = "x" if orientation == 'vertical' else "y"
interval = getattr(self.dataLim, f"interval{axis_name}")
axis = self._axis_map[axis_name]
positions = axis.convert_units(positions)
Expand Down
4 changes: 3 additions & 1 deletion lib/matplotlib/axes/_axes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ class Axes(_AxesBase):
notch: bool | None = ...,
sym: str | None = ...,
vert: bool | None = ...,
orientation: Literal["vertical", "horizontal"] = ...,
whis: float | tuple[float, float] | None = ...,
positions: ArrayLike | None = ...,
widths: float | ArrayLike | None = ...,
Expand Down Expand Up @@ -382,7 +383,8 @@ class Axes(_AxesBase):
positions: ArrayLike | None = ...,
*,
widths: float | ArrayLike | None = ...,
vert: bool = ...,
vert: bool | None = ...,
orientation: Literal["vertical", "horizontal"] = ...,
patch_artist: bool = ...,
shownotches: bool = ...,
showmeans: bool = ...,
Expand Down
2 changes: 2 additions & 0 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2918,6 +2918,7 @@ def boxplot(
notch: bool | None = None,
sym: str | None = None,
vert: bool | None = None,
orientation: Literal["vertical", "horizontal"] = "vertical",
whis: float | tuple[float, float] | None = None,
positions: ArrayLike | None = None,
widths: float | ArrayLike | None = None,
Expand Down Expand Up @@ -2950,6 +2951,7 @@ def boxplot(
notch=notch,
sym=sym,
vert=vert,
orientation=orientation,
whis=whis,
positions=positions,
widths=widths,
Expand Down
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 07b29f7

Please sign in to comment.