Skip to content

Correctly set temporary pdf/pgf backends #27869

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/matplotlib/backends/backend_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2755,8 +2755,7 @@ def savefig(self, figure=None, **kwargs):
raise ValueError(f"No figure {figure}")
figure = manager.canvas.figure
# Force use of pdf backend, as PdfPages is tightly coupled with it.
with cbook._setattr_cm(figure, canvas=FigureCanvasPdf(figure)):
figure.savefig(self, format="pdf", **kwargs)
figure.savefig(self, format="pdf", backend="pdf", **kwargs)

def get_pagecount(self):
"""Return the current number of pages in the multipage pdf file."""
Expand Down
39 changes: 19 additions & 20 deletions lib/matplotlib/backends/backend_pgf.py
Original file line number Diff line number Diff line change
Expand Up @@ -987,26 +987,25 @@ def savefig(self, figure=None, **kwargs):
raise ValueError(f"No figure {figure}")
figure = manager.canvas.figure

with cbook._setattr_cm(figure, canvas=FigureCanvasPgf(figure)):
width, height = figure.get_size_inches()
if self._n_figures == 0:
self._write_header(width, height)
else:
# \pdfpagewidth and \pdfpageheight exist on pdftex, xetex, and
# luatex<0.85; they were renamed to \pagewidth and \pageheight
# on luatex>=0.85.
self._file.write(
(
r'\newpage'
r'\ifdefined\pdfpagewidth\pdfpagewidth'
fr'\else\pagewidth\fi={width}in'
r'\ifdefined\pdfpageheight\pdfpageheight'
fr'\else\pageheight\fi={height}in'
'%%\n'
).encode("ascii")
)
figure.savefig(self._file, format="pgf", **kwargs)
self._n_figures += 1
width, height = figure.get_size_inches()
if self._n_figures == 0:
self._write_header(width, height)
else:
# \pdfpagewidth and \pdfpageheight exist on pdftex, xetex, and
# luatex<0.85; they were renamed to \pagewidth and \pageheight
# on luatex>=0.85.
self._file.write(
(
r'\newpage'
r'\ifdefined\pdfpagewidth\pdfpagewidth'
fr'\else\pagewidth\fi={width}in'
r'\ifdefined\pdfpageheight\pdfpageheight'
fr'\else\pageheight\fi={height}in'
'%%\n'
).encode("ascii")
)
figure.savefig(self._file, format="pgf", backend="pgf", **kwargs)
self._n_figures += 1

def get_pagecount(self):
"""Return the current number of pages in the multipage pdf file."""
Expand Down