PythonTeX (Python + Latex) 错误 savefig() in PythonteX Gallery



我正在学习Python,Latex,并发现了PythonTeX的奇妙包的存在,它允许将Python代码添加到TeX中。此站点上有一个图库文件:

https://ctan.org/pkg/pythontex

如果您下载所有软件包文件,则有"图库"tex源文件,我正在编译该文件进行测试。还有简单的绘图生成代码(简短的摘录(:

You may want to use matplotlib's PGF backend when creating plots.
begin{pylabblock}
rc('text', usetex=True)
rc('font', family='serif')
rc('font', size=10.0)
rc('legend', fontsize=10.0)
rc('font', weight='normal')
x = linspace(0, 10)
figure(figsize=(4, 2.5))
plot(x, sin(x), label='$sin(x)$')
xlabel(r'$xmathrm{-axis}$')
ylabel(r'$ymathrm{-axis}$')
legend(loc='lower right')
savefig('myplot.pdf', bbox_inches='tight')
end{pylabblock}

在执行这部分时,特别是最后一行:

savefig('myplot.pdf', bbox_inches='tight')

我收到这组令人讨厌的错误(对不起大小(:

----  Messages for pylab:default:5  ----
Traceback (most recent call last):
* PythonTeX stderr - error on line 173:
File "pythontex-files-pythontex_gallery_withiffileexistscondonplotpylab_default_5.py", line 62, in <module>
savefig('myplot.pdf', bbox_inches='tight')
File "C:pythonlibsite-packagesmatplotlibpyplot.py", line 722, in savefig
res = fig.savefig(*args, **kwargs)
File "C:pythonlibsite-packagesmatplotlibfigure.py", line 2180, in savefig
self.canvas.print_figure(fname, **kwargs)
File "C:pythonlibsite-packagesmatplotlibbackend_bases.py", line 2056, in print_figure
**kwargs)
File "C:pythonlibsite-packagesmatplotlibbackendsbackend_pdf.py", line 2503, in print_pdf
self.figure.draw(renderer)
File "C:pythonlibsite-packagesmatplotlibartist.py", line 38, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:pythonlibsite-packagesmatplotlibfigure.py", line 1709, in draw
renderer, self, artists, self.suppressComposite)
File "C:pythonlibsite-packagesmatplotlibimage.py", line 135, in _draw_list_compositing_images
a.draw(renderer)
File "C:pythonlibsite-packagesmatplotlibartist.py", line 38, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:pythonlibsite-packagesmatplotlibaxes_base.py", line 2647, in draw
mimage._draw_list_compositing_images(renderer, self, artists)
File "C:pythonlibsite-packagesmatplotlibimage.py", line 135, in _draw_list_compositing_images
a.draw(renderer)
File "C:pythonlibsite-packagesmatplotlibartist.py", line 38, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:pythonlibsite-packagesmatplotlibaxis.py", line 1205, in draw
renderer)
File "C:pythonlibsite-packagesmatplotlibaxis.py", line 1150, in _get_tick_bboxes
for tick in ticks if tick.label1.get_visible()],
File "C:pythonlibsite-packagesmatplotlibaxis.py", line 1150, in <listcomp>
for tick in ticks if tick.label1.get_visible()],
File "C:pythonlibsite-packagesmatplotlibtext.py", line 890, in get_window_extent
bbox, info, descent = self._get_layout(self._renderer)
File "C:pythonlibsite-packagesmatplotlibtext.py", line 291, in _get_layout
ismath="TeX" if self.get_usetex() else False)
File "C:pythonlibsite-packagesmatplotlibbackends_backend_pdf_ps.py", line 47, in get_text_width_height_descent
s, fontsize, renderer=self)
File "C:pythonlibsite-packagesmatplotlibtexmanager.py", line 450, in get_text_width_height_descent
page, = dvi
File "C:pythonlibsite-packagesmatplotlibdviread.py", line 243, in __iter__
while self._read():
File "C:pythonlibsite-packagesmatplotlibdviread.py", line 301, in _read
self._dtable[byte](self, byte)
File "C:pythonlibsite-packagesmatplotlibdviread.py", line 161, in wrapper
return method(self, *[f(self, byte-min) for f in get_args])
File "C:pythonlibsite-packagesmatplotlibdviread.py", line 441, in _fnt_def
self._fnt_def_real(k, c, s, d, a, l)
File "C:pythonlibsite-packagesmatplotlibdviread.py", line 446, in _fnt_def_real
tfm = _tfmfile(fontname)
File "C:pythonlibsite-packagesmatplotlibdviread.py", line 1063, in _fontfile
filename = find_tex_file(texname + suffix)
File "C:pythonlibsite-packagesmatplotlibdviread.py", line 1052, in find_tex_file
result = cbook._check_and_log_subprocess(cmd, _log, **kwargs)
File "C:pythonlibsite-packagesmatplotlibcbook__init__.py", line 2127, in _check_and_log_subprocess
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs)
File "C:pythonlibsubprocess.py", line 472, in run
with Popen(*popenargs, **kwargs) as process:
File "C:pythonlibsubprocess.py", line 775, in __init__
restore_signals, start_new_session)
File "C:pythonlibsubprocess.py", line 1178, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] Systém nemůže nalézt uvedený soubor
--------------------------------------------------
PythonTeX:  pythontex_gallery_withIfFileExistsCondOnPlot - 1 error(s), 0 warning(s)

有趣的是,这个问题是特定于机器的。我还有另外两台(更旧!(机器,可以正常处理此代码。一个在 Windows 10 上,另一个在 Windows 7 上。问题出在另一台同样装有 Windows 10 的设备上。

我在某处读到此错误是由 Python 在其名称中包含非 ASCII 符号的目录中生成的。看到这一点,我把python放在C:\根中。另外,我已经相应地设置了PATH变量(已经在安装程序中(。

如何解决这个问题?

如果需要完整(工作(的 MWE,请通知我,而不是链接到 CTAN 站点。如果是这样,我尝试将官方文件最小化到尽可能少的代码量并编辑问题。

感谢您的任何帮助,

问候,托马斯

编辑:就在我收到评论时,发现在我的Windows 10用户目录中生成了一个文件夹.matplotlib(在其路径中包含非ACII字母(。比较机器之间的 .matplotlib 内容,我发现如果成功调用 savefig(( 函数,则有很多文件,否则只有两个。

现在我确定,这样做的原因是在使用 PythonTeX 和 matplotlib 时,会在用户主目录中创建一个 .matplotlib 目录,这(在我的情况下(是一个错误的路径。

有没有办法,如何更改创建此目录的位置?

我的问题是由"C:\texlive\2019\bin\win32"文件夹中缺少二进制文件"kpsewhich.exe"引起的,该文件提供了有关使用哪种字体的matplotlib信息。由于在提到的文件夹在我的 PATH 中之前,将文件"kpsewhich.exe"从不同的设备(也适用于 Windows 2019 上的 TeXlive 10(复制到已经提到的文件夹就足够了。

相关内容

  • 没有找到相关文章

最新更新