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

Commit035403a

Browse files
committed
fix boxplot legend entries
1 parente5562bb commit035403a

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

‎lib/matplotlib/axes/_axes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4007,6 +4007,9 @@ def boxplot(self, x, notch=None, sym=None, vert=None, whis=None,
40074007
if'color'inboxprops:
40084008
boxprops['edgecolor']=boxprops.pop('color')
40094009

4010+
iflabels:
4011+
boxprops['label']=labels
4012+
40104013
# if non-default sym value, put it into the flier dictionary
40114014
# the logic for providing the default symbol ('b+') now lives
40124015
# in bxp in the initial value of flierkw
@@ -4319,13 +4322,16 @@ def do_patch(xs, ys, **kwargs):
43194322
do_box=do_patchifpatch_artistelsedo_plot
43204323
boxes.append(do_box(box_x,box_y,**box_kw))
43214324
# draw the whiskers
4325+
whisker_kw.setdefault('label','_nolegend_')
43224326
whiskers.append(do_plot(whis_x,whislo_y,**whisker_kw))
43234327
whiskers.append(do_plot(whis_x,whishi_y,**whisker_kw))
43244328
# maybe draw the caps
43254329
ifshowcaps:
4330+
cap_kw.setdefault('label','_nolegend_')
43264331
caps.append(do_plot(cap_x,cap_lo,**cap_kw))
43274332
caps.append(do_plot(cap_x,cap_hi,**cap_kw))
43284333
# draw the medians
4334+
median_kw.setdefault('label','_nolegend_')
43294335
medians.append(do_plot(med_x,med_y,**median_kw))
43304336
# maybe draw the means
43314337
ifshowmeans:
@@ -4338,6 +4344,7 @@ def do_patch(xs, ys, **kwargs):
43384344
means.append(do_plot([pos], [stats['mean']],**mean_kw))
43394345
# maybe draw the fliers
43404346
ifshowfliers:
4347+
flier_kw.setdefault('label','_nolegend_')
43414348
flier_x=np.full(len(stats['fliers']),pos,dtype=np.float64)
43424349
flier_y=stats['fliers']
43434350
fliers.append(do_plot(flier_x,flier_y,**flier_kw))

‎lib/matplotlib/tests/test_legend.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,3 +1391,35 @@ def test_legend_nolabels_draw():
13911391
plt.plot([1,2,3])
13921392
plt.legend()
13931393
assertplt.gca().get_legend()isnotNone
1394+
1395+
1396+
deftest_boxplot_legend():
1397+
# Test that boxplot legends handles are patches
1398+
# and labels are generated from boxplot's labels parameter.
1399+
fig,axs=plt.subplots()
1400+
A=5*np.random.rand(100,1)
1401+
B=10*np.random.rand(100,1)-5
1402+
C=7*np.random.rand(100,1)-5
1403+
labels= ['a','b','c']
1404+
1405+
bp0=axs.boxplot(A,positions=[0],patch_artist=True,labels=labels[0])
1406+
bp1=axs.boxplot(B,positions=[1],patch_artist=True,labels=labels[1])
1407+
bp2=axs.boxplot(C,positions=[2],patch_artist=True,labels=labels[2])
1408+
# red, blue, green
1409+
colors= [(1.0,0.0,0.0,1), (0.0,0.0,1.0,1), (0.0,0.5,0.0,1)]
1410+
box_list= [bp0,bp1,bp2]
1411+
# Set colors to the boxes
1412+
lbl_index=0
1413+
forb_plot,colorinzip(box_list,colors):
1414+
forpatchinb_plot['boxes']:
1415+
patch.set_color(color)
1416+
lbl_index+=1
1417+
1418+
legend=axs.legend()
1419+
index=0
1420+
foriinlegend.legend_handles:
1421+
assertisinstance(i,mpl.patches.Rectangle)
1422+
asserti.get_facecolor()==colors[index]
1423+
asserti.get_edgecolor()==colors[index]
1424+
asserti.get_label()==labels[index]
1425+
index+=1

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp