Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 700047a

Browse filesBrowse files
committed
Fix tight_layout() on "canvasless" figures.
This patch avoids a deprecation warning ("unexpected argument 'dpi'") on `Figure().tight_layout()`. To do so we stop passing `dpi` to `print_foo`, which is actually fine because `print_figure` already sets the figure dpi to whatever value we will want, so `print_foo` can just read `self.figure.dpi`. (The pgf backend already handles that correctly.)
1 parent 92b4bc3 commit 700047a
Copy full SHA for 700047a

File tree

Expand file treeCollapse file tree

6 files changed

+37
-16
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

6 files changed

+37
-16
lines changed
Open diff view settings
Collapse file
+6Lines changed: 6 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
The *dpi* parameter of ``FigureCanvas.print_foo`` printers is deprecated
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
The `~.Figure.savefig` machinery already took care of setting the figure dpi
4+
to the desired value, so ``print_foo`` can directly read it from there. Not
5+
passing *dpi* to ``print_foo`` allows clearer detection of unused parameters
6+
passed to `~.Figure.savefig`.
Collapse file

‎lib/matplotlib/backend_bases.py‎

Copy file name to clipboardExpand all lines: lib/matplotlib/backend_bases.py
+11-9Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,7 +1573,7 @@ def _draw(renderer): raise Done(renderer)
15731573
print_method = getattr(
15741574
figure.canvas._get_output_canvas(None, fmt), f"print_{fmt}")
15751575
try:
1576-
print_method(io.BytesIO(), dpi=figure.dpi)
1576+
print_method(io.BytesIO())
15771577
except Done as exc:
15781578
renderer, = figure._cachedRenderer, = exc.args
15791579
return renderer
@@ -2235,14 +2235,16 @@ def print_figure(
22352235
cl_state = self.figure.get_constrained_layout()
22362236
self.figure.set_constrained_layout(False)
22372237
try:
2238-
result = print_method(
2239-
filename,
2240-
dpi=dpi,
2241-
facecolor=facecolor,
2242-
edgecolor=edgecolor,
2243-
orientation=orientation,
2244-
bbox_inches_restore=_bbox_inches_restore,
2245-
**kwargs)
2238+
# _get_renderer may change the figure dpi (as vector formats
2239+
# force the figure dpi to 72), so we need to set it again here.
2240+
with cbook._setattr_cm(self.figure, dpi=dpi):
2241+
result = print_method(
2242+
filename,
2243+
facecolor=facecolor,
2244+
edgecolor=edgecolor,
2245+
orientation=orientation,
2246+
bbox_inches_restore=_bbox_inches_restore,
2247+
**kwargs)
22462248
finally:
22472249
if bbox_inches and restore_bbox:
22482250
restore_bbox()
Collapse file

‎lib/matplotlib/backends/backend_pdf.py‎

Copy file name to clipboardExpand all lines: lib/matplotlib/backends/backend_pdf.py
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2701,10 +2701,13 @@ def get_default_filetype(self):
27012701
return 'pdf'
27022702

27032703
@_check_savefig_extra_args
2704+
@_api.delete_parameter("3.4", "dpi")
27042705
def print_pdf(self, filename, *,
2705-
dpi=72, # dpi to use for images
2706+
dpi=None, # dpi to use for images
27062707
bbox_inches_restore=None, metadata=None):
27072708

2709+
if dpi is None: # always use this branch after deprecation elapses.
2710+
dpi = self.figure.get_dpi()
27082711
self.figure.set_dpi(72) # there are 72 pdf points to an inch
27092712
width, height = self.figure.get_size_inches()
27102713
if isinstance(filename, PdfPages):
Collapse file

‎lib/matplotlib/backends/backend_ps.py‎

Copy file name to clipboardExpand all lines: lib/matplotlib/backends/backend_ps.py
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,11 +829,14 @@ def print_ps(self, outfile, *args, **kwargs):
829829
def print_eps(self, outfile, *args, **kwargs):
830830
return self._print_ps(outfile, 'eps', *args, **kwargs)
831831

832+
@_api.delete_parameter("3.4", "dpi")
832833
def _print_ps(
833834
self, outfile, format, *args,
834-
dpi=72, metadata=None, papertype=None, orientation='portrait',
835+
dpi=None, metadata=None, papertype=None, orientation='portrait',
835836
**kwargs):
836837

838+
if dpi is None: # always use this branch after deprecation elapses.
839+
dpi = self.figure.get_dpi()
837840
self.figure.set_dpi(72) # Override the dpi kwarg
838841

839842
dsc_comments = {}
Collapse file

‎lib/matplotlib/backends/backend_svg.py‎

Copy file name to clipboardExpand all lines: lib/matplotlib/backends/backend_svg.py
+5-2Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,9 +1343,12 @@ def print_svgz(self, filename, *args, **kwargs):
13431343
return self.print_svg(gzipwriter)
13441344

13451345
@_check_savefig_extra_args
1346-
def _print_svg(self, filename, fh, *, dpi=72, bbox_inches_restore=None,
1346+
@_api.delete_parameter("3.4", "dpi")
1347+
def _print_svg(self, filename, fh, *, dpi=None, bbox_inches_restore=None,
13471348
metadata=None):
1348-
self.figure.set_dpi(72.0)
1349+
if dpi is None: # always use this branch after deprecation elapses.
1350+
dpi = self.figure.get_dpi()
1351+
self.figure.set_dpi(72)
13491352
width, height = self.figure.get_size_inches()
13501353
w, h = width * 72, height * 72
13511354

Collapse file

‎lib/matplotlib/backends/backend_template.py‎

Copy file name to clipboardExpand all lines: lib/matplotlib/backends/backend_template.py
+7-3Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,13 @@ def draw(self):
204204

205205
def print_foo(self, filename, *args, **kwargs):
206206
"""
207-
Write out format foo. The dpi, facecolor and edgecolor are restored
208-
to their original values after this call, so you don't need to
209-
save and restore them.
207+
Write out format foo.
208+
209+
This method is normally called via `.Figure.savefig` and
210+
`.FigureCanvasBase.print_figure`, which take care of setting the figure
211+
facecolor, edgecolor, and dpi to the desired output values, and will
212+
restore them to the original values. Therefore, `print_foo` does not
213+
need to handle these settings.
210214
"""
211215
self.draw()
212216

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.