Skip to content

Commit 12527a7

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 12527a7

File tree

2 files changed

+13
-31
lines changed

2 files changed

+13
-31
lines changed

lib/matplotlib/axes/_axes.py

-3
Original file line numberDiff line numberDiff line change
@@ -4004,9 +4004,6 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
40044004
if 'color' in boxprops:
40054005
boxprops['edgecolor'] = boxprops.pop('color')
40064006

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

lib/matplotlib/tests/test_legend.py

+13-28
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)