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

ENH: rely on non-rectangular patch paths rather than bboxes for legend auto-placing (fix #9580)#9598

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
jklymak merged 3 commits intomatplotlib:mainfromafvincent:enh_cleverer_auto_legend
Dec 15, 2022

Conversation

afvincent
Copy link
Contributor

@afvincentafvincent commentedOct 28, 2017
edited
Loading

PR Summary

This PR addresses issue#9580.

Rationale

Currently, when using the legend auto-placing (NB: default since 2.0), one can get significantly surprising results as
orig_test
where things are OK with the two upper subplots but the two lower ones are not.

As@anntzer explained in#9580, this is because "step*" histogram types return a singlePolygon patch while non-"step*" types return a list ofRectangle patches. As the legend auto-placing algorithm relies on thebbox of the patches, it can significantly overestimates the actual patch area (e.g. as in the case displayed above).This PR simply suggests to use the path that defines non-rectangular patches instead of theirbbox in the legend auto-placing algorithm. By doing this the algorithm should handle these patches as well (or bad) as any Line2D instances.

At least, it fixes the previous example...
with_cleverer_auto_legend

Remaining concerns

NB: the followings points are kind of related actually.

Is it not kind of a nuke to treat every non-rectangular patches as a path?

A less radical change may be to keep the current bbox-based approach for some other patches likeEllipse,Circle, etc. where the issue stated above is less “severe” and the bbox provide a quite good approximation of the area that is covered by the patch.

What about speed?

Avery crude and genuine benchmark with a ~ 1000 bin-histogram is given in#9580 and suggest than this change is not likely to be the performance bottleneck of legend. If I am correct, at least for histograms, the new approach should not be really more computationally expensive than the cost of the non-step* histogram types, where the legend bbox is tested againsteveryRectangle patch the histogram is made of, isn't it?However, it may be worth to check that performance does not become a issue for plot with very high amounts of “complex” patches.

PR Checklist

  • Has Pytest style unit tests
  • Code is PEP 8 compliant
  • [?] Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

This breaks the previously “incorrect” behavior, and there will be no way to recover it.@tacaswellShould I add a proper entry inapi_changes.rst about it?

@afvincentafvincent added this to thev2.2 milestoneOct 28, 2017
@afvincent
Copy link
ContributorAuthor

afvincent commentedOct 28, 2017
edited
Loading

Tests arecoming! here. I tried to usenon image ones, but to do so, I did not find anything more straightforward than relying on assertions about theBboxes of the legends... If there was something way more clever to do, I am interested.

In both tests, the leftmost subplot is designed to be used as the reference case. FYI, here are the pictures (not used to perform the tests) that I used for the two tests:
test_legend_auto4
test_legend_auto5
while formerly the results would have been:
old_behavior_test_legend_auto4
old_behavior_test_legend_auto5

@afvincentafvincent changed the title[WiP] ENH: rely on non-rectangular patch paths rather than bboxes for legend auto-placing (fix #9580)ENH: rely on non-rectangular patch paths rather than bboxes for legend auto-placing (fix #9580)Oct 28, 2017
@afvincentafvincentforce-pushed theenh_cleverer_auto_legend branch frome97e93c to414d4f8CompareOctober 28, 2017 05:23
@afvincent
Copy link
ContributorAuthor

Sorry, I let an F-string from former iterations in the previous commit (3.6+ ❤️)... I fixed it and rebased to squash the one character-commit.

Copy link
Contributor

@anntzeranntzer left a comment

Choose a reason for hiding this comment

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

I think it would be good to add a "the auto-legend locator algorithm has been tweaked" note in the API changes, without necessarily going into the details (so if we change it again before the next release it would be OK to leave it as it is), mostly to leave a breadcrumb if anyone is confused by the change in behavior.

I honestly wouldn't worry about the loss of a clearly less optimal behavior.

@Kojoley
Copy link
Member

So with this change it could place a legend inside a non-solid bar (if the bar is big enough)?

@anntzer
Copy link
Contributor

good point... (but I think being fully in a solid polygon is less annoying that crossing some boundaries)

@jklymak
Copy link
Member

You could check if the legend was inside any polygons as well though your second test would fail. But I don’t think most of the time you want the legend inside a drawn polygon.

@tacaswell
Copy link
Member

How much computation time does this add? With the 'auto' behavior now the default this is a bit of a hot-path for people updating plots.

@afvincent
Copy link
ContributorAuthor

afvincent commentedOct 30, 2017
edited
Loading

@anntzer: I added an entry inapi_changes, quite minimalistic as you suggested ;).

@tacaswell: I tried to write a benchmark scripthere. It seems to me that the performance impact is barely noticeable (between +0.9% and +1.3% of execution time in the examples of the benchmark).However TBH I know very little about profiling (and monkey-patching withmock too...), so I may have misunderstand the profiler (pprofile) output. Any advice or suggestion are welcome if there were better ways to do this kind of study 🐑.

The plots used for benchmarking look like:

  • current (former?) algorithm
    benchmark_of_former_method_000

  • suggested (new?) algorithm
    benchmark_of_new_method_000

and they are more intended to give hard time to thelegend locator than to be representative of real-life plots. (Well,I am not used to plot histograms with ~ 5000 bins or plots with 500Polygon patches with ~ 10 edges each.)

One can see that the behavior with the patch collection is still quite surprising (not to say wrong...), and there is something weird with the plot coordinates. (I guess that I missed sometransform trick.)

@Kojoley and@jklymak: I am still looking into the consequences of this PR in the kind of situations that you suggested!

Edit: Added some values from the benchmark results.

@afvincent
Copy link
ContributorAuthor

Putting back the [WiP] mark, as some legitimate questions about the consequences of this PR have been made and still need to be evaluated.

@jklymak
Copy link
Member

Personally, I think putting a legend inside an artist is an OKfallback, but only if it can't be put outside an artist. I wonder if a "better" way to go would be doing the old bounding box check first and then, only if that fails for all possible locations, do the intersecting-vertex check. I think that gets better behaviour but still preserves the spirit of this PR.

@jklymak
Copy link
Member

Also, looking at the above, I wonder if the check order needs to be changed. I'd far prefer the legend top center than bottom center in the first test.

@afvincent
Copy link
ContributorAuthor

@jklymak About the check order: I was having the same thought (but usually@tacaswell is a bit reluctant with API changes just for “aesthetic” reasons 😈 :P). Anyway, other things are broken/weird with the check order, for example "(centered) right" seems to be evaluated twice but not even in successive positions...

For your other idea, that is an interesting idea, I'll try to give it a shot when I have more time (likely not today...).

@jklymak
Copy link
Member

ping@afvincent I still think this is a great enhancement....

@afvincent
Copy link
ContributorAuthor

@jklymak I did not forget it ;). It is simply that day-job and other more natural events (Thomas Fire, I am speaking of you 😈) have required a significant share of my attention during the past few weeks... Hopefully I will have more time to take care of this (and the other PRs I opened recently) during the second half of December. If something is really urgent, I do not mind friendly take overs. But I guess that the next “deadline” was pushed to 2.2, which gives a bit of time, does it not?

@jklymak
Copy link
Member

Yeah, but 2.2 has over 900 issues, so we will be busy ;-)

@jklymak
Copy link
Member

ping@afvincent real-life bah! I still think this should go in, maybe after some mods...

@jklymakjklymak modified the milestones:needs sorting,v3.0Mar 21, 2018
@afvincent
Copy link
ContributorAuthor

@jklymak Hearing you loud and clear, and this PR is still in my on-going work list ;).

I am still trying to find a useful way to benchmark that change (IIRC@tacaswell was a bit worry about that change). Besides, there was also a comment in the thread about the fact that the default order used to explore the possible position might not be the most “intuitive” one (whatever this means :P), but I guess that we might re-evaluate it for 3.0 if that is relevant.

@jklymak
Copy link
Member

As someone who is slowing down drawing as fast as others are speeding it up, I disagree that perfomance is a huge issue (within reason). If a user needs performance, they shouldn't use automatic convenience functions, i.e in this case, they can specify the location kwarg. But thats just my vote; I appreciate that its also the default, so it has the potential to slow people down by default is an issue.

My only problem w/ this PR is that I think its possible to theoretically put the legend inside the data curve, and that doesn't appeal.

At the very least, this could get its own value for the location kwarg. "superauto"?

@anntzer
Copy link
Contributor

Would rather not introduce a new location keyword, keep things lean, yada yada. If it's just a 1% slowdown I wouldn't worry to much about it...

jklymak and timhoffm reacted with thumbs up emoji

@jklymakjklymak modified the milestones:v3.0,v3.1Jul 9, 2018
@jklymakjklymak removed this from thev3.1.0 milestoneFeb 9, 2019
@jklymakjklymak added this to theunassigned milestoneFeb 9, 2019
@jklymak
Copy link
Member

ping@afvincent - I think this one was quite close if you have time...

@jklymakjklymak removed the stale labelJul 16, 2019
@jklymakjklymak marked this pull request as draftJuly 23, 2020 16:42
@jklymak
Copy link
Member

Annual ping@afvincent...

albertscholtz, taciturnaxolotl, LogvinovLeon, RuslanGrigoryev, Hunam6, haayhappen, trampfox, benrhodes26, codeananda, antoinebrl, and 21 more reacted with laugh emoji

@story645story645 modified the milestones:unassigned,needs sortingOct 6, 2022
@tacaswell
Copy link
Member

I took the liberty of rebasing this and think it is ready for re-review.

@tacaswelltacaswell marked this pull request as ready for reviewDecember 5, 2022 22:06
@tacaswelltacaswellforce-pushed theenh_cleverer_auto_legend branch from5f5c798 to694e2f4CompareDecember 9, 2022 20:23
@tacaswell
Copy link
Member

@jklymak Can you please make the call if this should go in or be pushed to 3.8?

Copy link
Member

@jklymakjklymak left a comment

Choose a reason for hiding this comment

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

This seems like it should work and not be too much of a speed drain (beyond what best already does). Thanks for rebasing@tacaswell.

@jklymakjklymak merged commitd8bb1a5 intomatplotlib:mainDec 15, 2022
@jklymak
Copy link
Member

I took the liberty of doing a squash merge

raphaelquast pushed a commit to raphaelquast/matplotlib that referenced this pull requestMar 16, 2023
…d auto-placing (fixmatplotlib#9580) (matplotlib#9598)* use path rather than bbox for non rectangular patches* Add tests* Add a short breadcrumb note in api_changes
@zhondori
Copy link

It took 5 years to merge this PR 🙂

mugagambi, anitasv, AlexandrePh, Hunam6, gorarakelyan, dangtony98, outrunthewolf, zejiran, remilepriol, ayushpahwa, and 109 more reacted with laugh emojiadamduncanshop, iangrayhazzard, luckened, hailelagi, cgarrovillo, siliconfire, sonal-ridecell, tizmagik, dev-inately, MahsaAbadian, and 3 more reacted with hooray emojiMyzel394, luckened, and cgarrovillo reacted with confused emojiaaimio, neonbhai, iMostfa, avedmala, iangrayhazzard, luckened, zaini, cgarrovillo, sonal-ridecell, jackson-ball, and rafagcfever reacted with rocket emojiHenriqueNas, GravO8, pratikdaigavane, MartinCura, barrutko, bader-tayeb, neonbhai, ignacio-chiazzo, iMostfa, iangrayhazzard, and 6 more reacted with eyes emoji

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

@tacaswelltacaswelltacaswell approved these changes

@anntzeranntzeranntzer approved these changes

@jklymakjklymakjklymak approved these changes

+1 more reviewer

@ser112rser112rser112r approved these changes

Reviewers whose approvals may not affect merge requirements
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
v3.7.0
Development

Successfully merging this pull request may close these issues.

8 participants
@afvincent@Kojoley@anntzer@jklymak@tacaswell@zhondori@ser112r@story645

[8]ページ先頭

©2009-2025 Movatter.jp