Skip to content

Use setattr_cm more. #25316

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
Feb 24, 2023
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
6 changes: 1 addition & 5 deletions lib/matplotlib/backends/backend_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2766,12 +2766,8 @@ 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.
try:
orig_canvas = figure.canvas
figure.canvas = FigureCanvasPdf(figure)
with cbook._setattr_cm(figure, canvas=FigureCanvasPdf(figure)):
figure.savefig(self, format="pdf", **kwargs)
finally:
figure.canvas = orig_canvas

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

try:
orig_canvas = figure.canvas
figure.canvas = FigureCanvasPgf(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)
Expand All @@ -1036,11 +1033,8 @@ def savefig(self, figure=None, **kwargs):
br'\else\pageheight\fi=%ain'
b'%%\n' % (width, height)
)

figure.savefig(self._file, format="pgf", **kwargs)
self._n_figures += 1
finally:
figure.canvas = orig_canvas

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