Skip to content

Commit 211f84f

Browse files
committed
FIX: fix figure.set_dpi when pixel ratio not 1
1 parent 9ec4b95 commit 211f84f

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

lib/matplotlib/backends/backend_qt5.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,6 @@ def __init__(self, figure):
229229
self.figure = figure
230230
# We don't want to scale up the figure DPI more than once.
231231
# Note, we don't handle a signal for changing DPI yet.
232-
figure._original_dpi = figure.dpi
233232
self._update_figure_dpi()
234233
# In cases with mixed resolution displays, we need to be careful if the
235234
# dpi_ratio changes - in this case we need to resize the canvas

lib/matplotlib/figure.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ def __init__(self,
357357
self.dpi_scale_trans = Affine2D().scale(dpi, dpi)
358358
# do not use property as it will trigger
359359
self._dpi = dpi
360+
self._original_dpi = dpi
360361
self.bbox = TransformedBbox(self.bbox_inches, self.dpi_scale_trans)
361362

362363
self.frameon = frameon
@@ -964,13 +965,37 @@ def set_facecolor(self, color):
964965
"""
965966
self.patch.set_facecolor(color)
966967

967-
def set_dpi(self, val):
968+
def set_dpi(self, dpi):
968969
"""
969-
Set the resolution of the figure in dots-per-inch.
970+
Set the nominal resolution of the figure in dots-per-inch.
970971
971-
.. ACCEPTS: float
972-
"""
973-
self.dpi = val
972+
Parameters
973+
----------
974+
dpi : float
975+
976+
Notes
977+
-----
978+
Some backends have the concept of "hi-dpi" displays (or "Retina")
979+
where the resolution is doubled, but dimensions are traditionally
980+
specified as if the resolution were not doubled. Some Matplotlib
981+
GUI backends respect this doubling (i.e. Qt5Agg), but Matplotlib still
982+
specifies dimensions and dpi to matplotlib as if the resolution
983+
were not doubled. The default figure
984+
value is nominally 100 dpi, but displays that are deignated "hi-dpi"
985+
will internally double this to 200 dpi. We keep the "nominal" dpi
986+
because some computer set ups have one display at normal dpi, and a
987+
second at hi-dpi, so a nominal resolution must be stored to stop
988+
figures from doubling or halving is size when moved between displays.
989+
"""
990+
991+
# some backends set self._dpi to be a ratio times
992+
# self._original_dpi:
993+
ratio = self._dpi / self._original_dpi
994+
995+
# _original_dpi is the nominal dpi before any hi-dpi changes.
996+
self._original_dpi = dpi
997+
# calls _set_dpi with the actual display dpi...
998+
self.dpi = dpi * ratio
974999
self.stale = True
9751000

9761001
def set_figwidth(self, val, forward=True):

0 commit comments

Comments
 (0)