-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[Bug]: Global legend weird behaviors #26841
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
This looks like it is due to the signature on legend being too forgiving. https://matplotlib.org/stable/api/figure_api.html#matplotlib.figure.Figure.legend It looks like we try to infer what the positional argument means (it can be a list of strings to go with the auto-discovered artists if it is the only positional argument or a list of handles if there are two positional arguments). If you do not pass the labels as a keyword argument it will raise. What is going on here is we are going through a branch of matplotlib/lib/matplotlib/legend.py Lines 1336 to 1374 in e6af5a3
*args so the list of handles is being ignored (!!) so are zipping the labels passed in with the list of discovered handles which will be by axes hence the two lines on the second axes will be together at the end.
You should be getting a warning that looks like
I think we should escalate that warning to an exception because we have been warning for at least 6 years (but without saying it would break in the future), it is really bad to drop user input on the floor, and this behavior is deeply miss leading. In the mean time fig.legend(handles, labels, ncol=2, bbox_to_anchor=(0.15, 1.06), loc='upper left')
fig.legend(handles=handles, labels=labels, ncol=2, bbox_to_anchor=(0.15, 1.06), loc='upper left') both work correctly but he best invocation is probably fig.legend(handles=handles, ncol=2, bbox_to_anchor=(0.15, 1.06), loc='upper lef which lets the legend code extract the label from the Artist (which you set when you create the Artist so it can never gets out of sync). |
Thank you for your detailed analysis and explanation. ---- Replied Message ----FromThomas A ***@***.***>Date09/20/2023 21:54 ***@***.***> CcT.X. ***@***.***>***@***.***>SubjectRe: [matplotlib/matplotlib] [Bug]: Global legend weird behaviors (Issue #26841)
This looks like it is due to the signature on legend being too forgiving.
https://matplotlib.org/stable/api/figure_api.html#matplotlib.figure.Figure.legend
It looks like we try to infer what the positional argument means (it can be a list of strings to go with the auto-discovered artists if it is the only positional argument or a list of handles if there are two positional arguments). If you do not pass the labels as a keyword argument it will raise.
What is going on here is we are going through a branch of https://github.com/matplotlib/matplotlib/blob/e6af5a339c7cea73fa0aaa587e258ecb742d4bef/lib/matplotlib/legend.py#L1336-L1374 that never looks at *args so the list of handles is being ignored (!!) so are zipping the labels passed in with the list of discovered handles which will be by axes hence the two lines on the second axes will be together at the end.
You should be getting a warning that looks like
<ipython-input-12-f89da0bd97bd>:19: UserWarning: You have mixed positional and keyword arguments, some input may be discarded.
fig.legend(handles, labels=labels, ncol=2, bbox_to_anchor=(0.15, 1.06), loc='upper left')
I think we should escalate that warning to an exception because we have been warning for at least 6 years (but without saying it would break in the future), it is really bad to drop user input on the floor, and this behavior is deeply miss leading.
In the mean time
fig.legend(handles, labels, ncol=2, bbox_to_anchor=(0.15, 1.06), loc='upper left')
fig.legend(handles=handles, labels=labels, ncol=2, bbox_to_anchor=(0.15, 1.06), loc='upper left')
both work correctly but he best invocation is probably
fig.legend(handles=handles, ncol=2, bbox_to_anchor=(0.15, 1.06), loc='upper lef
which lets the legend code extract the label from the Artist (which you set when you create the Artist so it can never gets out of sync).
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: ***@***.***>
[
{
***@***.***": "http://schema.org",
***@***.***": "EmailMessage",
"potentialAction": {
***@***.***": "ViewAction",
"target": "#26841 (comment)",
"url": "#26841 (comment)",
"name": "View Issue"
},
"description": "View this Issue on GitHub",
"publisher": {
***@***.***": "Organization",
"name": "GitHub",
"url": "https://github.com"
}
}
]
|
Uh oh!
There was an error while loading. Please reload this page.
Bug summary
The color of the global legend for all subplots does not match the label colors.
Code for reproduction
Actual outcome
Expected outcome
The colors for legend F and legend F+ should be orange. But they are different.
Additional information
No response
Operating system
No response
Matplotlib Version
3.7.1
Matplotlib Backend
No response
Python version
No response
Jupyter version
No response
Installation
None
The text was updated successfully, but these errors were encountered: