Skip to content

Commit 3dfcb51

Browse files
committed
Partly revert #27711
This PR removes the propagation of `labels` to any artist legend labels. Other than the rest of the plotting functions `labels` is not used for legend labels but for xtick labels. This is only poorly documented via https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.bxp.html and in an [example](https://matplotlib.org/stable/gallery/statistics/boxplot_color.html). Whatever our way forward regarding the use of `labels` is, we should by no means propagate them simultaneously to xticks and legend entries. This coupling would cripple users' configurability and limit our ability to migrate to a clear API where legend labels and tick labels can be configured independently. Until we have sorted out a better API, the recommended solution for the original issue #20512 is to grab the artists returned from `boxplot()` and either `set_label()` on them or pass them to the legend call `ax.legend(handles, labels)`.
1 parent 585bdce commit 3dfcb51

File tree

2 files changed

+15
-32
lines changed

2 files changed

+15
-32
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3885,7 +3885,8 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
38853885
boxes are drawn with Patch artists.
38863886
38873887
labels : sequence, optional
3888-
Labels for each dataset (one per dataset).
3888+
Labels for each dataset (one per dataset). These are used for
3889+
x-tick labels; *not* for legend entries.
38893890
38903891
manage_ticks : bool, default: True
38913892
If True, the tick locations and labels will be adjusted to match
@@ -4004,9 +4005,6 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
40044005
if 'color' in boxprops:
40054006
boxprops['edgecolor'] = boxprops.pop('color')
40064007

4007-
if labels:
4008-
boxprops['label'] = labels
4009-
40104008
# if non-default sym value, put it into the flier dictionary
40114009
# the logic for providing the default symbol ('b+') now lives
40124010
# in bxp in the initial value of flierkw

lib/matplotlib/tests/test_legend.py

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,31 +1429,16 @@ def test_legend_text():
14291429
assert_allclose(leg_bboxes[1].bounds, leg_bboxes[0].bounds)
14301430

14311431

1432-
def test_boxplot_legend():
1433-
# Test that boxplot legends handles are patches
1434-
# and labels are generated from boxplot's labels parameter.
1435-
fig, axs = plt.subplots()
1436-
A = 5*np.random.rand(100, 1)
1437-
B = 10*np.random.rand(100, 1) - 5
1438-
C = 7*np.random.rand(100, 1) - 5
1439-
labels = ['a', 'b', 'c']
1440-
1441-
bp0 = axs.boxplot(A, positions=[0], patch_artist=True, labels=labels[0])
1442-
bp1 = axs.boxplot(B, positions=[1], patch_artist=True, labels=labels[1])
1443-
bp2 = axs.boxplot(C, positions=[2], patch_artist=True, labels=labels[2])
1444-
# red, blue, green
1445-
colors = [(1.0, 0.0, 0.0, 1), (0.0, 0.0, 1.0, 1), (0.0, 0.5, 0.0, 1)]
1446-
box_list = [bp0, bp1, bp2]
1447-
# Set colors to the boxes
1448-
lbl_index = 0
1449-
for b_plot, color in zip(box_list, colors):
1450-
for patch in b_plot['boxes']:
1451-
patch.set_color(color)
1452-
lbl_index += 1
1453-
1454-
legend = axs.legend()
1455-
for index, handle in enumerate(legend.legend_handles):
1456-
assert isinstance(handle, mpl.patches.Rectangle)
1457-
assert handle.get_facecolor() == colors[index]
1458-
assert handle.get_edgecolor() == colors[index]
1459-
assert handle.get_label() == labels[index]
1432+
def test_boxplot_labels():
1433+
# Test that boxplot(..., labels=) sets the tick labels but not legend entries
1434+
# This is not consistent with other plot types but is the current behavior.
1435+
fig, ax = plt.subplots()
1436+
np.random.seed(19680801)
1437+
data = np.random.random((10, 3))
1438+
bp = ax.boxplot(data, labels=['A', 'B', 'C'])
1439+
# Check that labels set the tick labels ...
1440+
assert [l.get_text() for l in ax.get_xticklabels()] == ['A', 'B', 'C']
1441+
# ... but not legend entries
1442+
handles, labels = ax.get_legend_handles_labels()
1443+
assert len(handles) == 0
1444+
assert len(labels) == 0

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy