-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
figure.legend can be called without arguments #2128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
It would be great to get @leejoon's thoughts on this, as the legend guy. I like this because it brings |
Should we reconsider the behavior of pyplot.legend()? IIRC, it operates upon gca(), not gcf(). I am inclined to keep it as gca(), but I just wanted to make sure a decision is made on it. |
Doesn't pyplot have figlegend which is the equivalent of this in pyplot? |
Yeah -- I think And as @jenshnielsen points out, we have |
Thank you for the confirmation. I've applied the same modification to |
I think it is good to merge. A few minor points.
Unless there is no objection of merging this, I will go ahead and merge it. |
Why not use OrderedDict from the standard library's collection module? |
I'm currently working on a re-factor of the legend code. It deprecates some of the Cheers, |
handles, labels = ax.get_legend_handles_labels() | ||
ldict = dict(ldict, **dict(zip(labels, handles))) | ||
|
||
handles, labels = ldict.values(), ldict.keys() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this need to be six-ified?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Plus, I would never trust the order to be the same from one call to the next. There is a reason why they say "order is never guaranteed".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is line 1165 legal python? I would have spelled it ldict.update(handles, labels)
? handles
and labels
only need to be iterable, so should be six-compliant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like legend entries with the same handle will clober each other. i've definitely used the same handle for multiple lines. i won't promise it was a good idea, but I would be irritated if MPL prevented me from doing so.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that line is legal python. It would be better written as an ldict.update() statement though. I think the idea is that we should expect that legend labels that are the same across axes objects are intended to be the same entry in the figure legend, which makes sense. However, this does confuse situations where the same label is used multiple times within an axes. At the very least, this code needs to be much better commented to explain its intentions and purpose.
@WeatherGod OrderedDict is new in python2.7. |
@mrterry yeah, so? We support python2.6. That particular line should really be doing a single call to items() and splitting things out that way, but you do have to watch out for the empty dictionary case. |
Just pointing out that it is not present in one of our supported python versions. I wanted to use it in setup.py, but got shot down. 😦 I really like OrderedDict. |
This having issues with pyparsing on 3.3. |
@sinhrks Are you still interested in working on this PR? |
pushing off to 1.5 due to issues with py3k and in-activity. |
Thank you for your comment and I apologize that I hadn't check it long. I've changed the internal logic not to use dict, but use list to retain the drawing order. About Legend Labels: My original intention is to create a figure legend easily in subplots which uses similar data, same type of chart and color schema. In such a case, "looks-identical" artists should have the same label, artist, colors, etc. Thus I'd like to ask on what condition 2 artist can be regarded as identical (or looks-similar enough to be omitted in figure legend) in most cases. In other detailed cases, handles and labels can be passed to legend explicitly. I've implemented a proto which also check artist type, color, edgecolor and facecolor are identical before skipping. I'll try to implement it once what attributes are checked. |
It looks like you have introduced a bunch of pep8 violations into the code. |
@sinhrks Are you still interested in working on this? |
@sinhrks This needs a rebase. Does it make sense to group artist by label? It might be worth getting the simpler change (making the API match that of |
Since it's been over 2 years since last contact, I'll pick this up. Merged and rebased onto my own branch, just getting tests to run etc. and I'll open a new PR when ready. |
I am closing this in favor of #7811 |
figure.legend() can be called without arguments.
In this case, figure legend artists will be created from all the contained axes legend handles and labels in the figure. The figure legend will merge handles if its label is identical, assuming that the same handlers is assigned to the same label names in subplots.