MCPcopy Index your code
hub / github.com/matplotlib/matplotlib / print_pdf

Method print_pdf

lib/matplotlib/backends/backend_pgf.py:833–864  ·  view source on GitHub ↗

Use LaTeX to compile a pgf generated figure to pdf.

(self, fname_or_fh, *, metadata=None, **kwargs)

Source from the content-addressed store, hash-verified

831 self._print_pgf_to_fh(file, **kwargs)
832
833 def print_pdf(self, fname_or_fh, *, metadata=None, **kwargs):
834 """Use LaTeX to compile a pgf generated figure to pdf."""
835 w, h = self.figure.get_size_inches()
836
837 info_dict = _create_pdf_info_dict('pgf', metadata or {})
838 pdfinfo = ','.join(
839 _metadata_to_str(k, v) for k, v in info_dict.items())
840
841 # print figure to pgf and compile it with latex
842 with TemporaryDirectory() as tmpdir:
843 tmppath = pathlib.Path(tmpdir)
844 self.print_pgf(tmppath / "figure.pgf", **kwargs)
845 (tmppath / "figure.tex").write_text(
846 "\n".join([
847 _DOCUMENTCLASS,
848 r"\usepackage[pdfinfo={%s}]{hyperref}" % pdfinfo,
849 r"\usepackage[papersize={%fin,%fin}, margin=0in]{geometry}"
850 % (w, h),
851 r"\usepackage{pgf}",
852 _get_preamble(),
853 r"\begin{document}",
854 r"\centering",
855 r"\input{figure.pgf}",
856 r"\end{document}",
857 ]), encoding="utf-8")
858 texcommand = mpl.rcParams["pgf.texsystem"]
859 cbook._check_and_log_subprocess(
860 [texcommand, "-interaction=nonstopmode", "-halt-on-error",
861 "-no-shell-escape", "figure.tex"], _log, cwd=tmpdir)
862 with ((tmppath / "figure.pdf").open("rb") as orig,
863 cbook.open_file_cm(fname_or_fh, "wb") as dest):
864 shutil.copyfileobj(orig, dest) # copy file contents to target
865
866 def print_png(self, fname_or_fh, **kwargs):
867 """Use LaTeX to compile a pgf figure to pdf and convert it to png."""

Callers 1

print_pngMethod · 0.95

Calls 7

print_pgfMethod · 0.95
_create_pdf_info_dictFunction · 0.90
_metadata_to_strFunction · 0.85
_get_preambleFunction · 0.85
get_size_inchesMethod · 0.80
joinMethod · 0.80
openMethod · 0.45

Tested by

no test coverage detected