Skip to content

Backport PR #23476: FIX: reset to original DPI in getstate #23478

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -2886,6 +2886,9 @@ def __getstate__(self):
# Set cached renderer to None -- it can't be pickled.
state["_cachedRenderer"] = None

# discard any changes to the dpi due to pixel ratio changes
state["_dpi"] = state.get('_original_dpi', state['_dpi'])

# add version information to the state
state['__mpl_version__'] = mpl.__version__

Expand Down
9 changes: 9 additions & 0 deletions lib/matplotlib/tests/test_figure.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import datetime
import io
from pathlib import Path
import pickle
import platform
from threading import Timer
from types import SimpleNamespace
Expand Down Expand Up @@ -1360,3 +1361,11 @@ def test_kwargs_pass():

assert fig.get_label() == 'whole Figure'
assert sub_fig.get_label() == 'sub figure'


def test_unpickle_with_device_pixel_ratio():
fig = Figure(dpi=42)
fig.canvas._set_device_pixel_ratio(7)
assert fig.dpi == 42*7
fig2 = pickle.loads(pickle.dumps(fig))
assert fig2.dpi == 42