Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d255e50

Browse files
jmehneJulian Mehne
authored andcommitted
Add test, adapt example.
1 parent c721a15 commit d255e50

3 files changed

Lines changed: 39 additions & 10 deletions

File tree

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,35 @@
1+
"""
2+
Showcases legend entries with more than one legend key.
3+
"""
14
import matplotlib.pyplot as plt
25
from matplotlib.legend_handler import HandlerTuple
36

47
fig, (ax1, ax2) = plt.subplots(2, 1)
8+
9+
# First plot: two legend keys for a single entry
510
p1 = ax1.scatter([1], [5], c='r', marker='s', s=100)
611
p2 = ax1.scatter([3], [2], c='b', marker='o', s=100)
12+
# `plot` returns a list, but we want the handle - thus the comma on the left
13+
p3, = ax1.plot([1, 5], [4, 4], 'm-d')
714

8-
l = ax1.legend([(p1, p2)], ['points'], scatterpoints=1,
9-
handler_map={tuple: HandlerTuple(ndivide=0)})
15+
# Assign two of the handles to the same legend entry by putting them in a tuple
16+
# and using a generic handler map (which would be used for any additional
17+
# tuples of handles like (p1, p3)).
18+
l = ax1.legend([(p1, p3), p2], ['two keys', 'one key'], scatterpoints=1,
19+
numpoints=1, handler_map={tuple: HandlerTuple(ndivide=0)})
1020

11-
ind = [1, 2, 3]
12-
pos1 = [1, 3, 2]
13-
neg1 = [2, 1, 4]
14-
width = [0.5, 0.5, 0.5]
21+
# Second plot: plot two bar charts on top of each other and change the padding
22+
# between the legend keys
23+
x_left = [1, 2, 3]
24+
y_pos = [1, 3, 2]
25+
y_neg = [2, 1, 4]
1526

16-
rpos1 = ax2.bar(ind, pos1, width=0.5, color='k', label='+1')
17-
rneg1 = ax2.bar(ind, neg1, width=0.5, color='w', hatch='///', label='-1')
27+
rneg = ax2.bar(x_left, y_neg, width=0.5, color='w', hatch='///', label='-1')
28+
rpos = ax2.bar(x_left, y_pos, width=0.5, color='k', label='+1')
1829

19-
l = ax2.legend([(rpos1, rneg1)], ['Test'],
20-
handler_map={(rpos1, rneg1): HandlerTuple(ndivide=0, pad=0.)})
30+
# Treat each legend entry differently by using specific `HandlerTuple`s
31+
l = ax2.legend([(rpos, rneg), (rneg, rpos)], ['pad!=0', 'pad=0'],
32+
handler_map={(rpos, rneg): HandlerTuple(ndivide=0),
33+
(rneg, rpos): HandlerTuple(ndivide=0, pad=0.)})
2134

2235
plt.show()
30.9 KB
Loading

lib/matplotlib/tests/test_legend.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import matplotlib.patches as mpatches
1919
import matplotlib.transforms as mtrans
2020

21+
from matplotlib.legend_handler import HandlerTuple
2122

2223
@image_comparison(baseline_images=['legend_auto1'], remove_text=True)
2324
def test_legend_auto1():
@@ -77,6 +78,21 @@ def test_labels_first():
7778
ax.legend(loc=0, markerfirst=False)
7879

7980

81+
@image_comparison(baseline_images=['legend_multiple_keys'], extensions=['png'],
82+
remove_text=True)
83+
def test_multiple_keys():
84+
# test legend entries with multiple keys
85+
fig = plt.figure()
86+
ax = fig.add_subplot(111)
87+
p1, = ax.plot([1, 2, 3], '-o')
88+
p2, = ax.plot([2, 3, 4], '-x')
89+
p3, = ax.plot([3, 4, 5], '-d')
90+
ax.legend([(p1, p2), (p2, p1), p3], ['two keys', 'pad=0', 'one key'],
91+
numpoints=1,
92+
handler_map={(p1, p2): HandlerTuple(ndivide=0),
93+
(p2, p1): HandlerTuple(ndivide=0, pad=0)})
94+
95+
8096
@image_comparison(baseline_images=['rgba_alpha'],
8197
extensions=['png'], remove_text=True)
8298
def test_alpha_rgba():

0 commit comments

Comments
 (0)