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 249a360

Browse filesBrowse files
authored
Merge pull request #10068 from matplotlib/auto-backport-of-pr-10064
Backport PR #10064 on branch v2.1.x
2 parents 8e7ffec + c0fc4e2 commit 249a360
Copy full SHA for 249a360

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
@@ -1325,43 +1325,9 @@ def _get_legend_handles_labels(axs, legend_handler_map=None):
13251325
handles = []
13261326
labels = []
13271327

1328-
def _in_handles(h, l):
1329-
# Method to check if we already have a given handle and label.
1330-
# Consider two handles to be the same if they share a label,
1331-
# color, facecolor, and edgecolor.
1332-
1333-
# Loop through each handle and label already collected
1334-
for f_h, f_l in zip(handles, labels):
1335-
if f_l != l:
1336-
continue
1337-
if type(f_h) != type(h):
1338-
continue
1339-
try:
1340-
if (colors.to_rgba_array(f_h.get_color()) !=
1341-
colors.to_rgba_array(h.get_color())).any():
1342-
continue
1343-
except AttributeError:
1344-
pass
1345-
try:
1346-
if (colors.to_rgba_array(f_h.get_facecolor()) !=
1347-
colors.to_rgba_array(h.get_facecolor())).any():
1348-
continue
1349-
except AttributeError:
1350-
pass
1351-
try:
1352-
if (colors.to_rgba_array(f_h.get_edgecolor()) !=
1353-
colors.to_rgba_array(h.get_edgecolor())).any():
1354-
continue
1355-
except AttributeError:
1356-
pass
1357-
return True
1358-
return False
1359-
13601328
for handle in _get_legend_handles(axs, legend_handler_map):
13611329
label = handle.get_label()
1362-
if (label and
1363-
not label.startswith('_') and
1364-
not _in_handles(handle, label)):
1330+
if (label and not label.startswith('_')):
13651331
handles.append(handle)
13661332
labels.append(label)
13671333
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

@@ -418,6 +419,21 @@ def test_nanscatter():
418419
ax.grid(True)
419420

420421

422+
def test_legend_repeatcheckok():
423+
fig, ax = plt.subplots()
424+
ax.scatter(0.0, 1.0, color='k', marker='o', label='test')
425+
ax.scatter(0.5, 0.0, color='r', marker='v', label='test')
426+
hl = ax.legend()
427+
hand, lab = mlegend._get_legend_handles_labels([ax])
428+
assert len(lab) == 2
429+
fig, ax = plt.subplots()
430+
ax.scatter(0.0, 1.0, color='k', marker='o', label='test')
431+
ax.scatter(0.5, 0.0, color='k', marker='v', label='test')
432+
hl = ax.legend()
433+
hand, lab = mlegend._get_legend_handles_labels([ax])
434+
assert len(lab) == 2
435+
436+
421437
@image_comparison(baseline_images=['not_covering_scatter'], extensions=['png'])
422438
def test_not_covering_scatter():
423439
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.