Skip to content

Commit c0041fb

Browse files
committed
FIX: when creating a canvas from a Figure use original dpi
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
1 parent 1ab3332 commit c0041fb

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1763,7 +1763,7 @@ def __init__(self, figure=None):
17631763
self.toolbar = None # NavigationToolbar2 will set me
17641764
self._is_idle_drawing = False
17651765
# We don't want to scale up the figure DPI more than once.
1766-
figure._original_dpi = figure.dpi
1766+
figure._original_dpi = getattr(figure, '_original_dpi', figure.dpi)
17671767
self._device_pixel_ratio = 1
17681768
super().__init__() # Typically the GUI widget init (if any).
17691769

lib/matplotlib/figure.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3003,12 +3003,10 @@ def _set_base_canvas(self):
30033003
This is used upon initialization of the Figure, but also
30043004
to reset the canvas when decoupling from pyplot.
30053005
"""
3006-
# check if we have changed the DPI due to hi-dpi screens
3007-
orig_dpi = getattr(self, '_original_dpi', self._dpi)
30083006
FigureCanvasBase(self) # Set self.canvas as a side-effect
3009-
# put it back to what it was
3010-
if orig_dpi != self._dpi:
3011-
self.dpi = orig_dpi
3007+
# undo any high-dpi scaling
3008+
if self._dpi != self._original_dpi:
3009+
self.dpi = self._original_dpi
30123010

30133011
def set_canvas(self, canvas):
30143012
"""

0 commit comments

Comments
 (0)