Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f50fe72

Browse files
authored
Merge pull request #19010 from anntzer/pgfprint
Inline _print_pdf_to_fh, _print_png_to_fh.
2 parents 2303c2b + dac2cdd commit f50fe72

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

lib/matplotlib/backends/backend_pgf.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,8 @@ def print_pgf(self, fname_or_fh, *args, **kwargs):
846846
file = codecs.getwriter("utf-8")(file)
847847
self._print_pgf_to_fh(file, *args, **kwargs)
848848

849-
def _print_pdf_to_fh(self, fh, *args, metadata=None, **kwargs):
849+
def print_pdf(self, fname_or_fh, *args, metadata=None, **kwargs):
850+
"""Use LaTeX to compile a pgf generated figure to pdf."""
850851
w, h = self.figure.get_figwidth(), self.figure.get_figheight()
851852

852853
info_dict = _create_pdf_info_dict('pgf', metadata or {})
@@ -879,29 +880,22 @@ def _print_pdf_to_fh(self, fh, *args, metadata=None, **kwargs):
879880
[texcommand, "-interaction=nonstopmode", "-halt-on-error",
880881
"figure.tex"], _log, cwd=tmpdir)
881882

882-
with (tmppath / "figure.pdf").open("rb") as fh_src:
883-
shutil.copyfileobj(fh_src, fh) # copy file contents to target
883+
with (tmppath / "figure.pdf").open("rb") as orig, \
884+
cbook.open_file_cm(fname_or_fh, "wb") as dest:
885+
shutil.copyfileobj(orig, dest) # copy file contents to target
884886

885-
def print_pdf(self, fname_or_fh, *args, **kwargs):
886-
"""Use LaTeX to compile a Pgf generated figure to PDF."""
887-
with cbook.open_file_cm(fname_or_fh, "wb") as file:
888-
self._print_pdf_to_fh(file, *args, **kwargs)
889-
890-
def _print_png_to_fh(self, fh, *args, **kwargs):
887+
def print_png(self, fname_or_fh, *args, **kwargs):
888+
"""Use LaTeX to compile a pgf figure to pdf and convert it to png."""
891889
converter = make_pdf_to_png_converter()
892890
with TemporaryDirectory() as tmpdir:
893891
tmppath = pathlib.Path(tmpdir)
894892
pdf_path = tmppath / "figure.pdf"
895893
png_path = tmppath / "figure.png"
896894
self.print_pdf(pdf_path, *args, **kwargs)
897895
converter(pdf_path, png_path, dpi=self.figure.dpi)
898-
with png_path.open("rb") as fh_src:
899-
shutil.copyfileobj(fh_src, fh) # copy file contents to target
900-
901-
def print_png(self, fname_or_fh, *args, **kwargs):
902-
"""Use LaTeX to compile a pgf figure to pdf and convert it to png."""
903-
with cbook.open_file_cm(fname_or_fh, "wb") as file:
904-
self._print_png_to_fh(file, *args, **kwargs)
896+
with png_path.open("rb") as orig, \
897+
cbook.open_file_cm(fname_or_fh, "wb") as dest:
898+
shutil.copyfileobj(orig, dest) # copy file contents to target
905899

906900
def get_renderer(self):
907901
return RendererPgf(self.figure, None)

0 commit comments

Comments
 (0)