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 d5e325d

Browse filesBrowse files
committed
FIX: drop logic to check for repeat labels
1 parent fb2c15b commit d5e325d
Copy full SHA for d5e325d

File tree

Expand file treeCollapse file tree

3 files changed

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

3 files changed

+37
-35
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-35Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,43 +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-
1354-
# Loop through each handle and label already collected
1355-
for f_h, f_l in zip(handles, labels):
1356-
if f_l != l:
1357-
continue
1358-
if type(f_h) != type(h):
1359-
continue
1360-
try:
1361-
if (colors.to_rgba_array(f_h.get_color()) !=
1362-
colors.to_rgba_array(h.get_color())).any():
1363-
continue
1364-
except AttributeError:
1365-
pass
1366-
try:
1367-
if (colors.to_rgba_array(f_h.get_facecolor()) !=
1368-
colors.to_rgba_array(h.get_facecolor())).any():
1369-
continue
1370-
except AttributeError:
1371-
pass
1372-
try:
1373-
if (colors.to_rgba_array(f_h.get_edgecolor()) !=
1374-
colors.to_rgba_array(h.get_edgecolor())).any():
1375-
continue
1376-
except AttributeError:
1377-
pass
1378-
return True
1379-
return False
1380-
13811349
for handle in _get_legend_handles(axs, legend_handler_map):
13821350
label = handle.get_label()
1383-
if (label and
1384-
not label.startswith('_') and
1385-
not _in_handles(handle, label)):
1351+
if (label and not label.startswith('_')):
13861352
handles.append(handle)
13871353
labels.append(label)
13881354
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.