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

Skip to content

Commit 4f56e1f

Browse files
authored
Rename boxplot's tick label parameter (#27901)
* rename tick label * fix docs * update label param on example docs * fix failing tests * move legend, fix doc and remove double line * fix docstring * fix docstring on axes.py
1 parent 5a4bb71 commit 4f56e1f

File tree

12 files changed

+59
-41
lines changed

12 files changed

+59
-41
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
``boxplot`` tick labels
2+
~~~~~~~~~~~~~~~~~~~~~~~
3+
The parameter *labels* has been renamed to *tick_labels* for clarity and consistency with `~.Axes.bar`.

galleries/examples/statistics/boxplot.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,23 @@
2828
# Demonstrate how to toggle the display of different elements:
2929

3030
fig, axs = plt.subplots(nrows=2, ncols=3, figsize=(6, 6), sharey=True)
31-
axs[0, 0].boxplot(data, labels=labels)
31+
axs[0, 0].boxplot(data, tick_labels=labels)
3232
axs[0, 0].set_title('Default', fontsize=fs)
3333

34-
axs[0, 1].boxplot(data, labels=labels, showmeans=True)
34+
axs[0, 1].boxplot(data, tick_labels=labels, showmeans=True)
3535
axs[0, 1].set_title('showmeans=True', fontsize=fs)
3636

37-
axs[0, 2].boxplot(data, labels=labels, showmeans=True, meanline=True)
37+
axs[0, 2].boxplot(data, tick_labels=labels, showmeans=True, meanline=True)
3838
axs[0, 2].set_title('showmeans=True,\nmeanline=True', fontsize=fs)
3939

40-
axs[1, 0].boxplot(data, labels=labels, showbox=False, showcaps=False)
40+
axs[1, 0].boxplot(data, tick_labels=labels, showbox=False, showcaps=False)
4141
tufte_title = 'Tufte Style \n(showbox=False,\nshowcaps=False)'
4242
axs[1, 0].set_title(tufte_title, fontsize=fs)
4343

44-
axs[1, 1].boxplot(data, labels=labels, notch=True, bootstrap=10000)
44+
axs[1, 1].boxplot(data, tick_labels=labels, notch=True, bootstrap=10000)
4545
axs[1, 1].set_title('notch=True,\nbootstrap=10000', fontsize=fs)
4646

47-
axs[1, 2].boxplot(data, labels=labels, showfliers=False)
47+
axs[1, 2].boxplot(data, tick_labels=labels, showfliers=False)
4848
axs[1, 2].set_title('showfliers=False', fontsize=fs)
4949

5050
for ax in axs.flat:

galleries/examples/statistics/boxplot_color.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
bplot = ax.boxplot(fruit_weights,
2828
patch_artist=True, # fill with color
29-
labels=labels) # will be used to label x-ticks
29+
tick_labels=labels) # will be used to label x-ticks
3030

3131
# fill with colors
3232
for patch, color in zip(bplot['boxes'], colors):

galleries/examples/statistics/bxp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
labels = list('ABCD')
2626

2727
# compute the boxplot stats
28-
stats = cbook.boxplot_stats(data, labels=labels, bootstrap=10000)
28+
stats = cbook.boxplot_stats(data, tick_labels=labels, bootstrap=10000)
2929

3030
# %%
3131
# After we've computed the stats, we can go through and change anything.

lib/matplotlib/axes/_axes.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3789,12 +3789,13 @@ def apply_mask(arrays, mask):
37893789
return errorbar_container # (l0, caplines, barcols)
37903790

37913791
@_preprocess_data()
3792+
@_api.rename_parameter("3.9", "labels", "tick_labels")
37923793
def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
37933794
positions=None, widths=None, patch_artist=None,
37943795
bootstrap=None, usermedians=None, conf_intervals=None,
37953796
meanline=None, showmeans=None, showcaps=None,
37963797
showbox=None, showfliers=None, boxprops=None,
3797-
labels=None, flierprops=None, medianprops=None,
3798+
tick_labels=None, flierprops=None, medianprops=None,
37983799
meanprops=None, capprops=None, whiskerprops=None,
37993800
manage_ticks=True, autorange=False, zorder=None,
38003801
capwidths=None):
@@ -3908,9 +3909,15 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
39083909
If `False` produces boxes with the Line2D artist. Otherwise,
39093910
boxes are drawn with Patch artists.
39103911
3911-
labels : sequence, optional
3912-
Labels for each dataset (one per dataset). These are used for
3913-
x-tick labels; *not* for legend entries.
3912+
tick_labels : list of str, optional
3913+
The tick labels of each boxplot.
3914+
Ticks are always placed at the box *positions*. If *tick_labels* is given,
3915+
the ticks are labelled accordingly. Otherwise, they keep their numeric
3916+
values.
3917+
3918+
.. versionchanged:: 3.9
3919+
Renamed from *labels*, which is deprecated since 3.9
3920+
and will be removed in 3.11.
39143921
39153922
manage_ticks : bool, default: True
39163923
If True, the tick locations and labels will be adjusted to match
@@ -3994,7 +4001,7 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
39944001
bootstrap = mpl.rcParams['boxplot.bootstrap']
39954002

39964003
bxpstats = cbook.boxplot_stats(x, whis=whis, bootstrap=bootstrap,
3997-
labels=labels, autorange=autorange)
4004+
tick_labels=tick_labels, autorange=autorange)
39984005
if notch is None:
39994006
notch = mpl.rcParams['boxplot.notch']
40004007
if vert is None:

lib/matplotlib/axes/_axes.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ class Axes(_AxesBase):
362362
showbox: bool | None = ...,
363363
showfliers: bool | None = ...,
364364
boxprops: dict[str, Any] | None = ...,
365-
labels: Sequence[str] | None = ...,
365+
tick_labels: Sequence[str] | None = ...,
366366
flierprops: dict[str, Any] | None = ...,
367367
medianprops: dict[str, Any] | None = ...,
368368
meanprops: dict[str, Any] | None = ...,

lib/matplotlib/cbook.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,7 +1126,8 @@ def _broadcast_with_masks(*args, compress=False):
11261126
return inputs
11271127

11281128

1129-
def boxplot_stats(X, whis=1.5, bootstrap=None, labels=None,
1129+
@_api.rename_parameter("3.9", "labels", "tick_labels")
1130+
def boxplot_stats(X, whis=1.5, bootstrap=None, tick_labels=None,
11301131
autorange=False):
11311132
r"""
11321133
Return a list of dictionaries of statistics used to draw a series of box
@@ -1161,10 +1162,14 @@ def boxplot_stats(X, whis=1.5, bootstrap=None, labels=None,
11611162
Number of times the confidence intervals around the median
11621163
should be bootstrapped (percentile method).
11631164
1164-
labels : array-like, optional
1165+
tick_labels : array-like, optional
11651166
Labels for each dataset. Length must be compatible with
11661167
dimensions of *X*.
11671168
1169+
.. versionchanged:: 3.9
1170+
Renamed from *labels*, which is deprecated since 3.9
1171+
and will be removed in 3.11.
1172+
11681173
autorange : bool, optional (False)
11691174
When `True` and the data are distributed such that the 25th and 75th
11701175
percentiles are equal, ``whis`` is set to (0, 100) such that the
@@ -1240,13 +1245,13 @@ def _compute_conf_interval(data, med, iqr, bootstrap):
12401245
X = _reshape_2D(X, "X")
12411246

12421247
ncols = len(X)
1243-
if labels is None:
1244-
labels = itertools.repeat(None)
1245-
elif len(labels) != ncols:
1246-
raise ValueError("Dimensions of labels and X must be compatible")
1248+
if tick_labels is None:
1249+
tick_labels = itertools.repeat(None)
1250+
elif len(tick_labels) != ncols:
1251+
raise ValueError("Dimensions of tick_labels and X must be compatible")
12471252

12481253
input_whis = whis
1249-
for ii, (x, label) in enumerate(zip(X, labels)):
1254+
for ii, (x, label) in enumerate(zip(X, tick_labels)):
12501255

12511256
# empty dict
12521257
stats = {}

lib/matplotlib/cbook.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def boxplot_stats(
135135
X: ArrayLike,
136136
whis: float | tuple[float, float] = ...,
137137
bootstrap: int | None = ...,
138-
labels: ArrayLike | None = ...,
138+
tick_labels: ArrayLike | None = ...,
139139
autorange: bool = ...,
140140
) -> list[dict[str, Any]]: ...
141141

lib/matplotlib/pyplot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2853,7 +2853,7 @@ def boxplot(
28532853
showbox: bool | None = None,
28542854
showfliers: bool | None = None,
28552855
boxprops: dict[str, Any] | None = None,
2856-
labels: Sequence[str] | None = None,
2856+
tick_labels: Sequence[str] | None = None,
28572857
flierprops: dict[str, Any] | None = None,
28582858
medianprops: dict[str, Any] | None = None,
28592859
meanprops: dict[str, Any] | None = None,
@@ -2884,7 +2884,7 @@ def boxplot(
28842884
showbox=showbox,
28852885
showfliers=showfliers,
28862886
boxprops=boxprops,
2887-
labels=labels,
2887+
tick_labels=tick_labels,
28882888
flierprops=flierprops,
28892889
medianprops=medianprops,
28902890
meanprops=meanprops,

lib/matplotlib/tests/test_axes.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8966,3 +8966,21 @@ def test_axes_clear_behavior(fig_ref, fig_test, which):
89668966

89678967
ax_ref.grid(True)
89688968
ax_test.grid(True)
8969+
8970+
8971+
def test_boxplot_tick_labels():
8972+
# Test the renamed `tick_labels` parameter.
8973+
# Test for deprecation of old name `labels`.
8974+
np.random.seed(19680801)
8975+
data = np.random.random((10, 3))
8976+
8977+
fig, axs = plt.subplots(nrows=1, ncols=2, sharey=True)
8978+
# Should get deprecation warning for `labels`
8979+
with pytest.warns(mpl.MatplotlibDeprecationWarning,
8980+
match='has been renamed \'tick_labels\''):
8981+
axs[0].boxplot(data, labels=['A', 'B', 'C'])
8982+
assert [l.get_text() for l in axs[0].get_xticklabels()] == ['A', 'B', 'C']
8983+
8984+
# Test the new tick_labels parameter
8985+
axs[1].boxplot(data, tick_labels=['A', 'B', 'C'])
8986+
assert [l.get_text() for l in axs[1].get_xticklabels()] == ['A', 'B', 'C']

lib/matplotlib/tests/test_cbook.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def test_results_whiskers_percentiles(self):
145145

146146
def test_results_withlabels(self):
147147
labels = ['Test1', 2, 'Aardvark', 4]
148-
results = cbook.boxplot_stats(self.data, labels=labels)
148+
results = cbook.boxplot_stats(self.data, tick_labels=labels)
149149
for lab, res in zip(labels, results):
150150
assert res['label'] == lab
151151

@@ -156,7 +156,7 @@ def test_results_withlabels(self):
156156
def test_label_error(self):
157157
labels = [1, 2]
158158
with pytest.raises(ValueError):
159-
cbook.boxplot_stats(self.data, labels=labels)
159+
cbook.boxplot_stats(self.data, tick_labels=labels)
160160

161161
def test_bad_dims(self):
162162
data = np.random.normal(size=(34, 34, 34))

lib/matplotlib/tests/test_legend.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,18 +1435,3 @@ def test_legend_text():
14351435
leg_bboxes.append(
14361436
leg.get_window_extent().transformed(ax.transAxes.inverted()))
14371437
assert_allclose(leg_bboxes[1].bounds, leg_bboxes[0].bounds)
1438-
1439-
1440-
def test_boxplot_labels():
1441-
# Test that boxplot(..., labels=) sets the tick labels but not legend entries
1442-
# This is not consistent with other plot types but is the current behavior.
1443-
fig, ax = plt.subplots()
1444-
np.random.seed(19680801)
1445-
data = np.random.random((10, 3))
1446-
bp = ax.boxplot(data, labels=['A', 'B', 'C'])
1447-
# Check that labels set the tick labels ...
1448-
assert [l.get_text() for l in ax.get_xticklabels()] == ['A', 'B', 'C']
1449-
# ... but not legend entries
1450-
handles, labels = ax.get_legend_handles_labels()
1451-
assert len(handles) == 0
1452-
assert len(labels) == 0

0 commit comments

Comments
 (0)