我尝试使用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')


但我得到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


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

%% [whole bunch of comments]
\begingroup%
\makeatletter%
\begin{pgfpicture}%
\pgfpathrectangle{\pgfpointorigin}{\pgfqpoint{10.000000in}{6.000000in}}%
\pgfusepath{use as bounding box}%


我不知道subprocesses.py中的OSError: No such file or directory与任何事情都有关系...我要保存的文件是可写的。我是在误解什么,还是我应该报告的错误?

最佳答案

在尝试运行示例脚本时,我也遇到了这个问题。 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)

确保您拥有xelatex,并且该文件位于PATH上,并将其用作默认的Latex命令(即,假设您使用的是Mac或Linux系统,which xelatex应该返回路径)。

关于matplotlib - matplotlib pgf:OSError:subprocess.py中没有这样的文件或目录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20000427/

10-14 07:54