Closed
Milestone
Description
Bug report
Legend handles and labels retrieval break as of 2.1.1. Trying to call ax.get_legend_handles_labels() results in a comparison ValueError being thrown. git bisect led to 4f594f4 being the start of the issue.
Code for reproduction
%matplotlib inline
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()Actual outcome
...in _in_handles(h, l)
1342 pass
1343 try:
-> 1344 if f_h.get_facecolor() != h.get_facecolor():
1345 continue
1346 except AttributeError:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()Expected outcome
Produce a scatter plot, as expected. This worked in 2.1.0.
Matplotlib version
- Operating system: Ubuntu 16.04 and macOS 10.13
- Matplotlib version: 2.1.1
- Matplotlib backend (
print(matplotlib.get_backend())): agg on both - Python version: 3.5.4
- Jupyter version (if applicable): 4.4.0
- Other libraries:
Metadata
Metadata
Assignees
Labels
No labels
Activity
jklymak commentedon Jan 16, 2018
Sorry this broke for you. Can you make a non-pandas example and I’ll try and look at what went wrong.
jklymak commentedon Jan 16, 2018
oops, sorry, this is a dupe and already fixed in master, and will soon be released on 2.1.2... See #10064
jakereps commentedon Jan 16, 2018
Thanks @jklymak, it seems that my first issue is resolved by the current
HEAD, but a new issue remains through using thecollections.OrderedDictlegend "gathering" method. Not sure if code changed internally to not support taking ordered dict values/keys items or if a cast to list or something was removed.Curious if this is a bug or not, as the
collections.OrderedDictway is (anecdotally) the go-to method for getting the handles/labels correctly grouped for customizing a legend.Same exact code as my first issue, but the final step of:
Which produces the following error output:
anntzer commentedon Jan 16, 2018
Looks like we need something like
jklymak commentedon Jan 16, 2018
@jakereps I can guess what you are doing, but can you make it explicit?
legendsis not defined yet in your code snippet.Again, it'd be clearer w/o pandas... No doubt @anntzer is correct, that we need to get rid of the
[:], but it'd be nice to have a complete example to test.[-]Retrieving legend handles and labels no longer works in 2.1.1[/-][+]OrderedDict legends no longer work 2.1[/+]jakereps commentedon Jan 16, 2018
Oh true, sorry about that, I copied from further into the real codebase, but forgot I didn't include the
legendvariable creation in the example.Here's the example sans
pandas:jklymak commentedon Jan 16, 2018
Thanks @jakereps if you are able to compare my PR that'd be great, but it works w/ the test above....
jakereps commentedon Jan 16, 2018
Will do! Currently installing the PR (dropped back to
2.1.0as a stopgap, so rebuilding the dev install).jakereps commentedon Jan 16, 2018
Perfect! Looks good to me! Thanks @jklymak and @anntzer!
dstansby commentedon Jan 18, 2018
Fixed via. #10263