Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Warning if handles and labels have a len mismatch#27004

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
rcomer merged 1 commit intomatplotlib:mainfromborgesaugusto:iss_24050
Oct 8, 2023

Conversation

borgesaugusto
Copy link
Contributor

@borgesaugustoborgesaugusto commentedOct 5, 2023
edited by rcomer
Loading

… when __ len __ exists for both

PR summary

Closes#24050 . Following the discussion there (And in PRs#24061 and#24063) a warning is issued when the handles and labels have __ len __() and there is a mismatch in its value. A test checks whether the warning is raised.

PR checklist

Copy link

@github-actionsgithub-actionsbot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks.

You can also join uson gitter for real-time discussion.

For details on testing, writing docs, and our review process, please seethe developer guide

We strive to be a welcoming and open project. Please follow ourCode of Conduct.

@@ -1337,6 +1337,12 @@ def _parse_legend_args(axs, *args, handles=None, labels=None, **kwargs):
_api.warn_external("You have mixed positional and keyword arguments, "
"some input may be discarded.")

if (hasattr(handles, "__len__") and
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Is it better do do thehasattr checks or catch (and ignore) theTypeError fro just trying?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I wondered the same. My reasoning was that since in the previous discussion it was agreed that only a warning should be raised , it made more sense to check for the attiribute rather than to do a try/except.
From a speed pov, I don't think there is going to be any big differences (try/except should be faster when __ len __ exists and viceversa). Thisstack discussioncould also be useful.

With try/except would be something like:

try:iflen(handles)!=len(labels):_api.warn_external(f"Mismatched number of handles and labels: "f"len(handles) ={len(handles)} "f"len(labels) ={len(labels)}")exceptTypeError:pass

vs the current impl

if (hasattr(handles,"__len__")and
hasattr(labels,"__len__")and
len(handles)!=len(labels)):
_api.warn_external(f"Mismatched number of handles and labels: "
f"len(handles) ={len(handles)} "
f"len(labels) ={len(labels)}")

I think using the hasattr() is cleaner, but I don't have a strong opinion about either.

In this file, hasattr is used once before:

# support parasite axes:
ifhasattr(ax,'parasites'):
foraxxinax.parasites:
handles_original+= [
*(aforainaxx._children
ifisinstance(a, (Line2D,Patch,Collection,Text))),
*axx.containers]

All other cases use try/except.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

+0.1 on the version of the PR.
IMHO here: "explicit is better than implicit" > "it's better to ask for foregiveness than for permission", because TypeError is quite generic.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I could see an argument for moving it into theif handles and labels: block, which is already the next check.

Thatshould make it so that both are at least iterable (escaping the one or both is None case), though I guess someone could pass e.g.labels=(str(x) for x in itertools.count()), which has nolen and is relying on thezip, so still needsome guard against unbounded inputs.

Copy link
Member

@timhoffmtimhoffmOct 5, 2023
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I think this is clearer. We're not obliged to jump extra hoops for user-friendly length checks if the user passes an iterable without alen implementation (which should be quite rare anyway).

@rcomer
Copy link
Member

Thanks@borgesaugusto, I think this is good to go. Are you confident to squash the commits down to one? If not we can handle that in the merge.

To squash, you can do aninteractive rebase. I strongly recommend saving a backup copy of your branch first if you have not done this (much) before.

borgesaugusto reacted with thumbs up emoji

… exists for bothNew check for the no len case. Now artists are stored
@rcomer
Copy link
Member

Thanks@borgesaugusto, this is in.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@tacaswelltacaswelltacaswell left review comments

@ksundenksundenksunden left review comments

@github-actionsgithub-actions[bot]github-actions[bot] left review comments

@timhoffmtimhoffmtimhoffm approved these changes

@rcomerrcomerrcomer approved these changes

Assignees
No one assigned
Projects
Milestone
v3.9.0
Development

Successfully merging this pull request may close these issues.

No error message in matplotlib.axes.Axes.legend() if there are more labels than handles
6 participants
@borgesaugusto@rcomer@tacaswell@ksunden@timhoffm@melissawm

[8]ページ先頭

©2009-2025 Movatter.jp