You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
eliflen(args) ==2: # 2 args: user defined handles and labels.
handles, labels=args[:2]
else:
raise_api.nargs_error('legend', '0-2', len(args))
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.
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"
}
}
]
Activity
tacaswell commentedon Sep 20, 2023
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
*argsso 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
both work correctly but he best invocation is probably
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).
imxtx commentedon Sep 20, 2023