Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork8.1k
FIX: when creating a canvas from a Figure use original dpi#30750
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
Open
tacaswell wants to merge2 commits intomatplotlib:mainChoose a base branch fromtacaswell:fix_hidpi_loop
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
+10 −8
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
When we upscale the DPI for high-dpi screens we stash the original dpi and thenset the figure dpi to the scaled version. If the same Figure instance isrepeatedly passed to a Canvas that support hi-dpi it would go into a loop wherethe scaled DPI is treated as the original dpi and infinitely increases.By grabbing `fig._original_dpi` (which we stash for exactly this reason) we canavoid this loop.closesmatplotlib#26380
Contributor
anntzer commentedNov 13, 2025
Maybe related to#27224? (I didn't actually check) |
The issue is that we reset the dpi but did not re-compute any of thebounding boxes derived from the dpi leading to the initial redraw eventsto use the wrong physical size on the screen.Closesmatplotlib#27224
MemberAuthor
tacaswell commentedNov 14, 2025
I found a clean place to test the second fix. |
timhoffm approved these changesNov 15, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR summary
When we upscale the DPI for high-dpi screens we stash the original dpi and then set the figure dpi to the scaled version. If the same Figure instance is repeatedly passed to a Canvas that support hi-dpi it would go into a loop where the scaled DPI is treated as the original dpi and infinitely increases.
By grabbing
fig._original_dpi(which we stash for exactly this reason) we can avoid this loop.closes#26380
This is a slightly more general version of what we had already done to support re-attaching Figures to pyplot.
This is covered by the tests for dpi ratio tests we added as part of re-attaching to Figures to pyplot (which my first version of this broke so I'm sure it is being hit).
There is a case that we should move the "re run with device ratio 1" logic into
CanvasBase, but any backend that does support scaling will take care of re-scaling the canvas on display so setting it back to 1 inCanvasBase.__init__seems like it will always be redundent for users and we have a hook to explicitly clean it up when we remove the Figure from pyplot.I suspect there is still a few more iterations before we have covered all the stress-cases on this, but this is another step in the right direction.
PR checklist