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 c125ab4

Browse filesBrowse files
committed
Merge pull request #10263 from jklymak/fix-legend-ordereddict
FIX: (re-allow) legend OrderedDict handles and labels... Conflicts: lib/matplotlib/legend.py - conflicts on patch, kept backported version
1 parent b1185ef commit c125ab4
Copy full SHA for c125ab4

File tree

Expand file treeCollapse file tree

2 files changed

+29
-8
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+29
-8
lines changed

‎lib/matplotlib/legend.py

Copy file name to clipboardExpand all lines: lib/matplotlib/legend.py
+11-8Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -581,14 +581,17 @@ def __init__(self, parent, handles, labels,
581581
setattr(self, name, value)
582582
del locals_view
583583
# trim handles and labels if illegal label...
584-
for label, handle in zip(labels[:], handles[:]):
585-
if (isinstance(label, six.string_types)
586-
and label.startswith('_')):
587-
warnings.warn('The handle {!r} has a label of {!r} which '
588-
'cannot be automatically added to the '
589-
'legend.'.format(handle, label))
590-
labels.remove(label)
591-
handles.remove(handle)
584+
_lab, _hand = [], []
585+
for label, handle in zip(labels, handles):
586+
if (isinstance(label, six.string_types) and
587+
label.startswith('_')):
588+
warnings.warn('The handle {!r} has a label of {!r} which '
589+
'cannot be automatically added to the '
590+
'legend.'.format(handle, label))
591+
else:
592+
_lab.append(label)
593+
_hand.append(handle)
594+
labels, handles = _lab, _hand
592595

593596
handles = list(handles)
594597
if len(handles) < 2:

‎lib/matplotlib/tests/test_legend.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_legend.py
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from unittest import mock
77
except ImportError:
88
import mock
9+
import collections
910
import numpy as np
1011
import pytest
1112

@@ -49,6 +50,23 @@ def test_legend_kwdocstrings():
4950
assert stleg == stfig
5051

5152

53+
def test_legend_ordereddict():
54+
# smoketest that ordereddict inputs work...
55+
56+
X = np.random.randn(10)
57+
Y = np.random.randn(10)
58+
labels = ['a'] * 5 + ['b'] * 5
59+
colors = ['r'] * 5 + ['g'] * 5
60+
61+
fig, ax = plt.subplots()
62+
for x, y, label, color in zip(X, Y, labels, colors):
63+
ax.scatter(x, y, label=label, c=color)
64+
65+
handles, labels = ax.get_legend_handles_labels()
66+
legend = collections.OrderedDict(zip(labels, handles))
67+
ax.legend(legend.values(), legend.keys(), loc=6, bbox_to_anchor=(1, .5))
68+
69+
5270
@image_comparison(baseline_images=['legend_auto1'], remove_text=True)
5371
def test_legend_auto1():
5472
'Test automatic legend placement'

0 commit comments

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