7

我尝试使用 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}%

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

4

1 回答 1

8

我在尝试运行示例脚本时也遇到了这个问题。问题出现在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应该返回一个路径)。

于 2013-12-01T20:35:25.583 回答