8000 FIX: fix figure.set_dpi when pixel ratio not 1 · matplotlib/matplotlib@f7745b1 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit f7745b1

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

File tree

6 files changed

+42
-11
lines changed

6 files changed

+42
-11
lines changed

lib/matplotlib/backends/backend_mixed.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def start_rasterizing(self):
9595
"""
9696

9797
# change the dpi of the figure temporarily.
98-
self.figure.set_dpi(self.dpi)
98+
self.figure.dpi = self.dpi
9999

100100
if self._bbox_inches_restore: # when tight bbox is used
101101
r = process_figure_for_rasterizing(self.figure,
@@ -141,7 +141,7 @@ def stop_rasterizing(self):
141141
self._rasterizing = False
142142

143143
# restore the figure dpi.
144-
self.figure.set_dpi(self._figdpi)
144+
self.figure.dpi = self._figdpi
145145

146146
if self._bbox_inches_restore: # when tight bbox is used
147147
r = process_figure_for_rasterizing(self.figure,

lib/matplotlib/backends/backend_pdf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2559,7 +2559,7 @@ def print_pdf(self, filename, *,
25592559
dpi=72, # dpi to use for images
25602560
bbox_inches_restore=None, metadata=None,
25612561
**kwargs):
2562-
self.figure.set_dpi(72) # there are 72 pdf points to an inch
2562+
self.figure.dpi = 72 # there are 72 pdf points to an inch
25632563
width, height = self.figure.get_size_inches()
25642564
if isinstance(filename, PdfPages):
25652565
file = filename._file

lib/matplotlib/backends/backend_ps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ def _print_ps(self, outfile, format, *args,
948948
elif orientation == 'portrait': isLandscape = False
949949
else: raise RuntimeError('Orientation must be "portrait" or "landscape"')
950950

951-
self.figure.set_dpi(72) # Override the dpi kwarg
951+
self.figure.dpi = 72 # Override the dpi kwarg
952952

953953
if rcParams['text.usetex']:
954954
self._print_figure_tex(outfile, format, dpi, facecolor, edgecolor,

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/backends/backend_svg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1214,7 +1214,7 @@ def print_svgz(self, filename, *args, **kwargs):
12141214

12151215
def _print_svg(
12161216
self, filename, fh, *, dpi=72, bbox_inches_restore=None, **kwargs):
1217-
self.figure.set_dpi(72.0)
1217+
self.figure.dpi = 72.0
12181218
width, height = self.figure.get_size_inches()
12191219
w, h = width * 72, height * 72
12201220

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)
0