Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a05f379

Browse files
committed
FIX: reset to original DPI in getstate
There is a bit of extra caution in extracting the original DPI just in case there is a canvas class that does something very funny and the `_orignial_dpi` attribute does not get set (it is set to the `Figure` instance in the `CanvasBase` init, not in the `Figure` init). closes #23471
1 parent 33a0599 commit a05f379

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

lib/matplotlib/figure.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3023,6 +3023,9 @@ def __getstate__(self):
30233023
# Set cached renderer to None -- it can't be pickled.
30243024
state["_cachedRenderer"] = None
30253025

3026+
# discard any changes to the dpi due to pixel ratio changes
3027+
state["_dpi"] = state.get('_original_dpi', state['_dpi'])
3028+
30263029
# add version information to the state
30273030
state['__mpl_version__'] = mpl.__version__
30283031

lib/matplotlib/tests/test_figure.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from datetime import datetime
33
import io
44
from pathlib import Path
5+
import pickle
56
import platform
67
from threading import Timer
78
from types import SimpleNamespace
@@ -1380,3 +1381,11 @@ def test_deepcopy():
13801381

13811382
assert ax.get_xlim() == (1e-1, 1e2)
13821383
assert fig2.axes[0].get_xlim() == (0, 1)
1384+
1385+
1386+
def test_unpickle_with_device_pixel_ratio():
1387+
fig = Figure(dpi=42)
1388+
fig.canvas._set_device_pixel_ratio(7)
1389+
assert fig.dpi == 42*7
1390+
fig2 = pickle.loads(pickle.dumps(fig))
1391+
assert fig2.dpi == 42

0 commit comments

Comments
 (0)