FIX: when creating a canvas from a Figure use original dpi#30750
Merged
ksunden merged 2 commits intomatplotlib:mainfrom Dec 11, 2025
Merged
FIX: when creating a canvas from a Figure use original dpi#30750ksunden merged 2 commits intomatplotlib:mainfrom
ksunden merged 2 commits intomatplotlib:mainfrom
Conversation
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 matplotlib#26380
Contributor
|
Maybe related to #27224? (I didn't actually check) |
8004c98 to
5c90416
Compare
The issue is that we reset the dpi but did not re-compute any of the bounding boxes derived from the dpi leading to the initial redraw events to use the wrong physical size on the screen. Closes matplotlib#27224
5c90416 to
973c99e
Compare
Member
Author
|
I found a clean place to test the second fix. |
timhoffm
approved these changes
Nov 15, 2025
QuLogic
approved these changes
Dec 11, 2025
ksunden
approved these changes
Dec 11, 2025
andreas16700
added a commit
to andreas16700/matplotlib
that referenced
this pull request
Jan 28, 2026
Includes Run 51-53 reports
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
Sign up for free
to 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