Skip to content

Commit 1bd9a79

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

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-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: 37 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,44 @@ 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+
This is nominal because some screens have "hi-dpi" or "Retina",
973+
where pixels are doubled. This dpi is the non-doubled value.
974+
975+
Setting the dpi different from your screen dpi makes the figure larger
976+
or smaller on the screen than the width and height specified in
977+
`~.Figure.get_width_height`.
978+
979+
Parameters
980+
----------
981+
dpi : float
982+
983+
Notes
984+
-----
985+
Some backends have the concept of "hi-dpi" displays (or "Retina")
986+
where the resolution is doubled, but dimensions are traditionally
987+
specified as if the resolution were not doubled. Some Matplotlib
988+
GUI backends respect this doubling (i.e. Qt5Agg), but Matplotlib still
989+
specifies dimensions and dpi to matplotlib as if the resolution
990+
were not doubled. The default figure
991+
value is nominally 100 dpi, but displays that are deignated "hi-dpi"
992+
will internally double this to 200 dpi. We keep the "nominal" dpi
993+
because some computer set ups have one display at normal dpi, and a
994+
second at hi-dpi, so a nominal resolution must be stored to stop
995+
figures from doubling or halving is size when moved between displays.
996+
"""
997+
998+
# some backends set self._dpi to be a ratio times
999+
# self._original_dpi:
1000+
ratio = self._dpi / self._original_dpi
1001+
1002+
# _original_dpi is the nominal dpi before any hi-dpi changes.
1003+
self._original_dpi = dpi
1004+
# calls _set_dpi with the actual display dpi...
1005+
self.dpi = dpi * ratio
9741006
self.stale = True
9751007

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

0 commit comments

Comments
 (0)