Skip to content

FIX: record original dpi when dpi set #24674

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

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 3 additions & 1 deletion lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -2176,7 +2176,7 @@ def set_dpi(self, val):
----------
val : float
"""
self._parent.dpi = val
self._parent.set_dpi(val)
self.stale = True

def _get_renderer(self):
Expand Down Expand Up @@ -3002,6 +3002,8 @@ def set_dpi(self, val):
val : float
"""
self.dpi = val
if hasattr(self, '_original_dpi'):
self._original_dpi = val
self.stale = True

def set_figwidth(self, val, forward=True):
Expand Down
9 changes: 9 additions & 0 deletions lib/matplotlib/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,15 @@ def test_subfigure_dpi():
assert fig.get_dpi() == 200


def test_set_dpi_save(tmp_path):
fig = plt.figure(figsize=(1, 2), dpi=100)
fig.set_dpi(63)
outname = tmp_path / 'dpi_test.png'
fig.savefig(outname, dpi='figure')
im = np.array(Image.open(outname))
assert np.shape(im) == (126, 63, 4)


@image_comparison(['test_subfigure_ss.png'], style='mpl20',
savefig_kwarg={'facecolor': 'teal'}, tol=0.02)
def test_subfigure_ss():
Expand Down