Skip to content

Commit 035403a

Browse files
committed
fix boxplot legend entries
1 parent e5562bb commit 035403a

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' in boxprops:
40084008
boxprops['edgecolor'] = boxprops.pop('color')
40094009

4010+
if labels:
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_patch if patch_artist else do_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
if showcaps:
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
if showmeans:
@@ -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
if showfliers:
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
assert plt.gca().get_legend() is not None
1394+
1395+
1396+
def test_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+
for b_plot, color in zip(box_list, colors):
1414+
for patch in b_plot['boxes']:
1415+
patch.set_color(color)
1416+
lbl_index += 1
1417+
1418+
legend = axs.legend()
1419+
index = 0
1420+
for i in legend.legend_handles:
1421+
assert isinstance(i, mpl.patches.Rectangle)
1422+
assert i.get_facecolor() == colors[index]
1423+
assert i.get_edgecolor() == colors[index]
1424+
assert i.get_label() == labels[index]
1425+
index += 1

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