Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3dfcb51

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 labelsbut for xtick labels. This is only poorly documented viahttps://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 meanspropagate them simultaneously to xticks and legend entries. This coupling would crippleusers' configurability and limit our ability to migrate to a clear API where legendlabels 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()` onthem or pass them to the legend call `ax.legend(handles, labels)`.
1 parent585bdce commit3dfcb51

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'inboxprops:
40054006
boxprops['edgecolor']=boxprops.pop('color')
40064007

4007-
iflabels:
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-
deftest_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-
forb_plot,colorinzip(box_list,colors):
1450-
forpatchinb_plot['boxes']:
1451-
patch.set_color(color)
1452-
lbl_index+=1
1453-
1454-
legend=axs.legend()
1455-
forindex,handleinenumerate(legend.legend_handles):
1456-
assertisinstance(handle,mpl.patches.Rectangle)
1457-
asserthandle.get_facecolor()==colors[index]
1458-
asserthandle.get_edgecolor()==colors[index]
1459-
asserthandle.get_label()==labels[index]
1432+
deftest_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()forlinax.get_xticklabels()]== ['A','B','C']
1441+
# ... but not legend entries
1442+
handles,labels=ax.get_legend_handles_labels()
1443+
assertlen(handles)==0
1444+
assertlen(labels)==0

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp