-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Figure legend not keeping Line2D marker #9155
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
Comments
Not sure what the issue is with figure legends, but you can achieve the same effect with an axes legend using something like:
(note Maybe that is an acceptable workaround. I am working on a way to make this automatically resize the parent axis but that'll be 2.2 or later (see #9082). |
@jklymak thank you for your suggestion - however it is a limited workaround, since axes only support one legend whereas figures support more of them. Above all, we are interested in finding out whether this is a bug or a feature :) |
@Juanlu001 @anhiga First, there is no difference between the for ax in self.axes:
ax_handles, ax_labels = ax.get_legend_handles_labels()
for h, l in zip(ax_handles, ax_labels):
if not in_handles(h, l):
handles.append(h)
labels.append(l) The solution to your mystery, however, is contained in
This is pretty hacky, and the undrscore means it could go away. Its a little strange to want the marker after the legend has been created. Why don't you just make the legend refer to something you've already drawn and keep track of what you've drawn? The legend doesn't seem like a good place to store the state of your plotting. |
Thank you for your answers.
You are right, we shouldn't rely on the legend to store our plotting state. I'm closing this. |
Bug report
Bug summary
When retrieving handles used in legend,
marker
property is not kept.Code for reproduction
Actual outcome
Expected outcome
When plotting an
Axes
legend,get_legend_handles_labels()
can be used, which does return the right marker. There isn't such a method inFigure
, so when usingLegend
methods or properties such aslegend.get_lines()
orlegend.legendHandles
, line2D marker isn't kept.Is there a better way of retrieving handles from a legend?
Am I doing something wrong?
Matplotlib version
cc @juanlu001
The text was updated successfully, but these errors were encountered: