-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Legend auto positioning takes text objects into account #16716
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
Conversation
… into issue_7869
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A little suspicious of this. We don’t know the text extents until a draw is done (I think?). Is there something in here that executed the draw so this works?
@jklymak Correct, the only way we know how big the text is going to be is to draw it (exact same problem we have with constrained layout / tight layout). The tests all have explicit I am a bit concerned that this PR is going to rapidly spiral out of scope. I don't think we want to start adding a second (third, depending on if you count tight_layout and constrained_layout is different things) thing that will internally force draws as that can easily start to add multiples to the draw time. On the other hand, we can just skip this logic when the figure isn't drawn yet, but that has the issue that saving twice will give different results and saving from the GUI will "work", but saving from a script won't. Unless there is a third option here (:crossed_fingers: ) I don't think this will be able to go in. Despite it making things better in some ways, it will make things worse in other ways. |
I think we could possibly get a provisional extent somehow which would be an overestimate. Might come in handy in other situations as well. Something like text_get_approx_extent that assume the text is font size high and len(str) * font size wide. |
That is true, @RiazCharania @Jordan711 I suggest having a look at how we do this in the autoticker / autolabeler where we use an estimate of the tick label size to pick the number of ticks. This may grow from "bug fix" to "new feature". |
quick question, running this code seems to work import matplotlib.pyplot as plt fig, ax = plt.subplots() fig.savefig("legend.png") is there a draw in there that im missing? |
Hmmm, interesting. Can you insert a print statement into the place where you get the extents and see what they are set to? Sometimes matplotlib is mysterious when things are drawn and its possible the legend is being updated after the text is drawn? |
running fig, ax = plt.subplots() fig.savefig("legend.png") with print(texts) line immediately after texts = ... in legend.py |
Actually (with approx. 10s thought...) I think we should just make Text.get_window_extent perform whatever drawing is necessary (creating a renderer if necessary), and then make sure the result is cached so we only do it once per string/renderer. |
yeah, but based on the above it seems to work anyway? |
Since this Pull Request has not been updated in 60 days, it has been marked "inactive." This does not mean that it will be closed, though it may be moved to a "Draft" state. This helps maintainers prioritize their reviewing efforts. You can pick the PR back up anytime - please ping us if you need a review or guidance to move the PR forward! If you do not plan on continuing the work, please let us know so that we can either find someone to take the PR over, or close it. |
This was done at #27469. |
PR Summary
This PR addresses issue #7869 Take text objects into account in legend auto positioning. The legend's auto positioning will now take into account text objects so that they will not overlap.
PR Checklist