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

Appearance settings

Commit 4f421c9

Browse filesBrowse files
committed
FIX: drop logic to check for repeat labels
1 parent 20e4897 commit 4f421c9
Copy full SHA for 4f421c9

File tree

Expand file treeCollapse file tree

3 files changed

+37
-47
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+37
-47
lines changed
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
`Figure.legend` no longer checks for repeated lines to ignore
2+
-------------------------------------------------------------
3+
4+
`matplotlib.Figure.legend` used to check if a line had the
5+
same label as an existing legend entry that also had the same line color
6+
or marker color and didn't add a new entry for that line.
7+
8+
This list of conditions was incomplete, didn't handle RGB tupples,
9+
didn't handle linewidths or linestyles etc.
10+
11+
This logic did not exist in `Axes.legend`. It was included (erroneously)
12+
in Matplotlib 2.1.1 when the legend argument parsing was unified
13+
[#9324](https://github.com/matplotlib/matplotlib/pull/9324). This change
14+
removes that check again.
15+
16+
This logic has also been dropped from `.Figure.legend`, where it
17+
was previously undocumented. Repeated
18+
lines with the same label will now each have an entry in the legend. If
19+
you do not want the duplicate entries, don't add a label to the line, or
20+
prepend the label with an underscore.

‎lib/matplotlib/legend.py

Copy file name to clipboardExpand all lines: lib/matplotlib/legend.py
+1-47Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,55 +1346,9 @@ def _get_legend_handles_labels(axs, legend_handler_map=None):
13461346
handles = []
13471347
labels = []
13481348

1349-
def _in_handles(h, l):
1350-
# Method to check if we already have a given handle and label.
1351-
# Consider two handles to be the same if they share a label,
1352-
# color, facecolor, and edgecolor.
1353-
print('l ', l, 'h ', h)
1354-
1355-
# Loop through each handle and label already collected
1356-
for f_h, f_l in zip(handles, labels):
1357-
print('fl ', f_l, 'fh ', f_h)
1358-
if f_l != l:
1359-
continue
1360-
if type(f_h) != type(h):
1361-
continue
1362-
try:
1363-
if (colors.to_rgba_array(f_h.get_color()) !=
1364-
colors.to_rgba_array(h.get_color())).any():
1365-
continue
1366-
except AttributeError:
1367-
pass
1368-
try:
1369-
if (colors.to_rgba_array(f_h.get_facecolor()) !=
1370-
colors.to_rgba_array(h.get_facecolor())).any():
1371-
continue
1372-
except AttributeError:
1373-
pass
1374-
try:
1375-
if (colors.to_rgba_array(f_h.get_edgecolor()) !=
1376-
colors.to_rgba_array(h.get_edgecolor())).any():
1377-
continue
1378-
except AttributeError:
1379-
pass
1380-
try:
1381-
if (f_h.get_linewidth() != h.get_linewidth()):
1382-
continue
1383-
except AttributeError:
1384-
pass
1385-
try:
1386-
if (f_h.get_linestyle() != h.get_linestyle()):
1387-
continue
1388-
except AttributeError:
1389-
pass
1390-
return True
1391-
return False
1392-
13931349
for handle in _get_legend_handles(axs, legend_handler_map):
13941350
label = handle.get_label()
1395-
if (label and
1396-
not label.startswith('_') and
1397-
not _in_handles(handle, label)):
1351+
if (label and not label.startswith('_')):
13981352
handles.append(handle)
13991353
labels.append(label)
14001354
return handles, labels

‎lib/matplotlib/tests/test_legend.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_legend.py
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import matplotlib.transforms as mtransforms
1717
import matplotlib.collections as mcollections
1818
from matplotlib.legend_handler import HandlerTuple
19+
import matplotlib.legend as mlegend
1920
import inspect
2021

2122

@@ -423,6 +424,21 @@ def test_nanscatter():
423424
ax.grid(True)
424425

425426

427+
def test_legend_repeatcheckok():
428+
fig, ax = plt.subplots()
429+
ax.scatter(0.0, 1.0, color='k', marker='o', label='test')
430+
ax.scatter(0.5, 0.0, color='r', marker='v', label='test')
431+
hl = ax.legend()
432+
hand, lab = mlegend._get_legend_handles_labels([ax])
433+
assert len(lab) == 2
434+
fig, ax = plt.subplots()
435+
ax.scatter(0.0, 1.0, color='k', marker='o', label='test')
436+
ax.scatter(0.5, 0.0, color='k', marker='v', label='test')
437+
hl = ax.legend()
438+
hand, lab = mlegend._get_legend_handles_labels([ax])
439+
assert len(lab) == 2
440+
441+
426442
@image_comparison(baseline_images=['not_covering_scatter'], extensions=['png'])
427443
def test_not_covering_scatter():
428444
colors = ['b', 'g', 'r']

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.