matplotlib pgf:OSError:subprocess.py中没有这样的文件或目录

Mar*_*ark 7 latex matplotlib pgf

我尝试使用matplotlib为LaTeX创建一个pgf文件:

from matplotlib.pyplot import subplots
from numpy import linspace
x = linspace(0, 100, 30)
fig, ax = subplots(figsize = (10, 6))
ax.scatter(x, x)
fig.tight_layout()
fig.savefig('/home/mark/dicp/python/figure.pgf')
Run Code Online (Sandbox Code Playgroud)

但我得到OSError: [Errno 2] No such file or directory:

Traceback (most recent call last):
  File "visualize/latex_figs.py", line 32, in <module>
    fig.savefig('/home/mark/dicp/python/figure.pgf')
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/figure.py", line 1421, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/backend_bases.py", line 2220, in print_figure
    **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/backend_bases.py", line 1957, in print_pgf
    return pgf.print_pgf(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_pgf.py", line 818, in print_pgf
    self._print_pgf_to_fh(fh, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_pgf.py", line 797, in _print_pgf_to_fh
    RendererPgf(self.figure, fh),
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_pgf.py", line 409, in __init__
    self.latexManager = LatexManagerFactory.get_latex_manager()
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_pgf.py", line 223, in get_latex_manager
    new_inst = LatexManager()
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_pgf.py", line 305, in __init__
    cwd=self.tmpdir)
  File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory
Run Code Online (Sandbox Code Playgroud)

它还会生成输出文件的这一部分:

%% [whole bunch of comments]
\begingroup%
\makeatletter%
\begin{pgfpicture}%
\pgfpathrectangle{\pgfpointorigin}{\pgfqpoint{10.000000in}{6.000000in}}%
\pgfusepath{use as bounding box}%
Run Code Online (Sandbox Code Playgroud)

我不明白OSError: No such file or directorysubprocesses.py中有什么与任何事情有关...我试图保存的文件是可写的.我误解了什么,或者这是我应该报告的错误?

Nat*_*son 7

我在尝试运行示例脚本时也遇到了这个问题.backend_pgf.py首次尝试使用默认LaTeX命令时会出现此问题.似乎PGF后端假定它应该xelatex默认使用.如果您和我的问题相同,那么您有两个选择:

  • 添加密钥"pgf.texsystem" : "pdflatex"(或lualatex其他)到您的matplotlib.rcParams.例如,将以下代码段添加到脚本的顶部:

    import matplotlib
    pgf_with_rc_fonts = {"pgf.texsystem": "pdflatex"}
    matplotlib.rcParams.update(pgf_with_rc_fonts)
    
    Run Code Online (Sandbox Code Playgroud)
  • 确保你有xelatex,并且它在你的身上,并将其PATH用作默认的latex命令(即假设你在Mac或Linux系统上,which xelatex应该返回一个路径).