Skip to content

FIX: (re-allow) legend OrderedDict handles and labels... - #10263

Merged
tacaswell merged 2 commits into
matplotlib:masterfrom
jklymak:fix-legend-ordereddict
Jan 17, 2018
Merged

tacaswell merged 2 commits into
matplotlib:masterfrom
jklymak:fix-legend-ordereddict

Conversation

@jklymak

@jklymak jklymak commented Jan 16, 2018

Copy link
Copy Markdown
Member

PR Summary

As reported in #10262, OrderedDict input no longer works in 2.1.1. i.e.

import collections
import matplotlib.pyplot as plt
import numpy as np

X = np.random.randn(10)
Y = np.random.randn(10)
labels = ['a'] * 5 + ['b'] * 5
colors = ['r'] * 5 + ['g'] * 5

fig, ax = plt.subplots()
for x, y, label, color in zip(X, Y, labels, colors):
    ax.scatter(x, y, label=label, c=color)

handles, labels = ax.get_legend_handles_labels()
legend = collections.OrderedDict(zip(labels, handles))
ax.legend(legend.values(), legend.keys(), loc=6, bbox_to_anchor=(1, .5))

This PR fixes that (thanks @anntzer) and adds a small test...

PR Checklist

  • Has Pytest style unit tests
  • Code is PEP 8 compliant

@jklymak jklymak added the Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. label Jan 16, 2018
@jklymak jklymak added this to the v2.1.2 milestone Jan 16, 2018
Comment thread lib/matplotlib/legend.py Outdated
else:
_lab.append(label)
_hand.append(handle)
label, handle = _lab, _hand

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plural!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh duh!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That certainly fixed it alright ;-)

@jklymak
jklymak force-pushed the fix-legend-ordereddict branch from 9cc2506 to 3d65ca1 Compare January 16, 2018 23:51
@anntzer
anntzer dismissed their stale review January 17, 2018 01:31

untypoed

@tacaswell
tacaswell merged commit 4f9458d into matplotlib:master Jan 17, 2018
@lumberbot-app

lumberbot-app Bot commented Jan 17, 2018

Copy link
Copy Markdown

There seem to be a conflict, please backport manually

@jklymak
jklymak deleted the fix-legend-ordereddict branch January 17, 2018 19:16
tacaswell added a commit that referenced this pull request Jan 18, 2018
FIX: (re-allow) legend OrderedDict handles and labels...
Conflicts:
	lib/matplotlib/legend.py
          - conflicts on patch, kept backported version
@tacaswell

Copy link
Copy Markdown
Member

backported to v2.1.x as c125ab4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants