Use LaTeX to compile a pgf generated figure to pdf.
(self, fname_or_fh, *, metadata=None, **kwargs)
| 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.""" |
no test coverage detected