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 3ea5f83

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

File tree

Expand file treeCollapse file tree

4 files changed

+41
-36
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+41
-36
lines changed

‎doc/api/api_changes.rst

Copy file name to clipboardExpand all lines: doc/api/api_changes.rst
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,29 @@ out what caused the breakage and how to fix it by updating your code.
1010
For new features that were added to Matplotlib, please see
1111
:ref:`whats-new`.
1212

13+
API Changes in 2.1.2
14+
====================
15+
16+
`Figure.legend` no longer checks for repeated lines to ignore
17+
-------------------------------------------------------------
18+
19+
`matplotlib.Figure.legend` used to check if a line had the
20+
same label as an existing legend entry. If it also had the same line color
21+
or marker color legend didn't add a new entry for that line. However, the
22+
list of conditions was incomplete, didn't handle RGB tupples,
23+
didn't handle linewidths or linestyles etc.
24+
25+
This logic did not exist in `Axes.legend`. It was included (erroneously)
26+
in Matplotlib 2.1.1 when the legend argument parsing was unified
27+
[#9324](https://github.com/matplotlib/matplotlib/pull/9324). This change
28+
removes that check in `Axes.legend` again to restore the old behavior.
29+
30+
This logic has also been dropped from `.Figure.legend`, where it
31+
was previously undocumented. Repeated
32+
lines with the same label will now each have an entry in the legend. If
33+
you do not want the duplicate entries, don't add a label to the line, or
34+
prepend the label with an underscore.
35+
1336
API Changes in 2.1.1
1437
====================
1538

‎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_figure.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_figure.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def test_figure_legend():
9090
axes[0].plot([0, 1], [0, 1], label='y', color='r')
9191
axes[0].plot([0, 1], [0.5, 0.5], label='y', color='k')
9292

93-
axes[1].plot([0, 1], [1, 0], label='y', color='r')
93+
axes[1].plot([0, 1], [1, 0], label='_y', color='r')
9494
axes[1].plot([0, 1], [0, 1], label='z', color='b')
9595
fig.legend()
9696

‎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.