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

Skip to content

Commit 7f4a73a

Browse files
committed
Update figure DPI when dpi_ratio changes
Similarly to resizeEvent, we tell _set_dpi to not change the canvas size since we are doing it ourselves.
1 parent f8cb5c9 commit 7f4a73a

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

lib/matplotlib/backends/backend_qt5.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ def save_figure(self, *args):
674674

675675
fname, filter = _getSaveFileName(self.parent,
676676
"Choose a filename to save to",
677-
start, filters, selectedFilter)
677+
start, filters, selectedFilter)
678678
if fname:
679679
if startpath == '':
680680
# explicitly missing key or empty str signals to use cwd

lib/matplotlib/backends/backend_qt5agg.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ def __init__(self, figure):
4343
# accordingly. We could watch for screenChanged events from Qt, but
4444
# the issue is that we can't guarantee this will be emitted *before*
4545
# the first paintEvent for the canvas, so instead we keep track of the
46-
# dpi_ratio value here and in paintEvent we resize the canvas if needed.
46+
# dpi_ratio value here and in paintEvent we resize the canvas if
47+
# needed.
4748
self._dpi_ratio_prev = None
4849

4950
def drawRectangle(self, rect):
@@ -68,9 +69,10 @@ def paintEvent(self, e):
6869
# As described in __init__ above, we need to be careful in cases with
6970
# mixed resolution displays if dpi_ratio is changing between painting
7071
# events.
71-
if self._dpi_ratio_prev is None:
72-
self._dpi_ratio_prev = self._dpi_ratio
73-
elif self._dpi_ratio != self._dpi_ratio_prev:
72+
if (self._dpi_ratio_prev is None or
73+
self._dpi_ratio != self._dpi_ratio_prev):
74+
# We need to update the figure DPI
75+
self._update_figure_dpi()
7476
# The easiest way to resize the canvas is to emit a resizeEvent
7577
# since we implement all the logic for resizing the canvas for
7678
# that event.
@@ -196,6 +198,10 @@ def __init__(self, figure):
196198
self.figure._original_dpi = self.figure.dpi
197199
self.figure.dpi = self._dpi_ratio * self.figure._original_dpi
198200

201+
def _update_figure_dpi(self):
202+
dpi = self._dpi_ratio * self.figure._original_dpi
203+
self.figure._set_dpi(dpi, forward=False)
204+
199205

200206
@_BackendQT5.export
201207
class _BackendQT5Agg(_BackendQT5):

lib/matplotlib/figure.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,11 +418,16 @@ def _get_axes(self):
418418
def _get_dpi(self):
419419
return self._dpi
420420

421-
def _set_dpi(self, dpi):
421+
def _set_dpi(self, dpi, forward=True):
422+
"""
423+
The forward kwarg is passed on to set_size_inches
424+
"""
422425
self._dpi = dpi
423426
self.dpi_scale_trans.clear().scale(dpi, dpi)
424-
self.set_size_inches(*self.get_size_inches())
427+
w, h = self.get_size_inches()
428+
self.set_size_inches(w, h, forward=forward)
425429
self.callbacks.process('dpi_changed', self)
430+
426431
dpi = property(_get_dpi, _set_dpi)
427432

428433
def get_tight_layout(self):

0 commit comments

Comments
 (0)