🌐 AI搜索 & 代理 主页
Skip to content

Commit 94055bc

Browse files
authored
Merge pull request #30750 from tacaswell/fix_hidpi_loop
FIX: when creating a canvas from a Figure use original dpi
2 parents fdf8995 + 973c99e commit 94055bc

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
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: 6 additions & 7 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
"""
@@ -3323,8 +3321,9 @@ def __setstate__(self, state):
33233321
self.__dict__ = state
33243322

33253323
# re-initialise some of the unstored state information
3326-
FigureCanvasBase(self) # Set self.canvas.
3327-
3324+
self._set_base_canvas()
3325+
# force the bounding boxes to respect current dpi
3326+
self.dpi_scale_trans.clear().scale(self._dpi)
33283327
if restore_to_pylab:
33293328
# lazy import to avoid circularity
33303329
import matplotlib.pyplot as plt

lib/matplotlib/tests/test_figure.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,6 +1688,9 @@ def test_unpickle_with_device_pixel_ratio():
16881688
assert fig.dpi == 42*7
16891689
fig2 = pickle.loads(pickle.dumps(fig))
16901690
assert fig2.dpi == 42
1691+
assert all(
1692+
[orig / 7 == restore for orig, restore in zip(fig.bbox.max, fig2.bbox.max)]
1693+
)
16911694

16921695

16931696
def test_gridspec_no_mutate_input():

0 commit comments

Comments
 (0)