Skip to main content
 首页 » 编程设计

python之尝试使用 matplotlib Latex 保存后记时出错

2024年10月25日10rubylouvre

当我尝试将 python 图保存为 PostScript 时,当使用 Latex 和 xfrac 包时,出现错误,我可以将图保存为其他格式,但不能保存为 PostScript

这是我使用的代码..

import matplotlib 
import matplotlib.pyplot as plt 
 
# Use LaTeX for rendering 
matplotlib.rcParams["text.usetex"] = True 
# load the xfrac package 
matplotlib.rcParams["text.latex.preamble"].append(r'\usepackage{xfrac}') 
 
fig = plt.figure() 
ax = fig.add_subplot(111) 
ax.plot([0,1],[1,0]) 
ax.text(.5, .5, r'$\sfrac{1}{2}$') 
plt.savefig('111.ps') 

这是我得到的错误(如果我不使用 xfrac 包我不会得到错误)

LaTeX was not able to process your file:     
Here is the full report generated by LaTeX:  
 
This is pdfTeX, Version 3.14159265-2.6-1.40.15 (TeX Live 2014) (preloaded format=latex) 
 restricted \write18 enabled. 
entering extended mode 
(/tmp/tmp0Nr4Ze.tex 
LaTeX2e <2014/05/01> 
Babel <3.9k> and hyphenation patterns for 79 languages loaded. 
(/home/users/MyName/Local/Latex/texmf-dist/tex/latex/base/article.cls 
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class 
(/home/users/MyName/Local/Latex/texmf-dist/tex/latex/base/size10.clo)) 
(/home/users/MyName/Local/Latex/texmf-dist/tex/latex/type1cm/type1cm.sty) 
 (/home/users/MyName/Local/Latex/texmf-dist/tex/latex/psnfss/helvet.sty 
(/home/users/MyName/Local/Latex/texmf-dist/tex/latex/graphics/keyval.sty) 
) (/home/users/MyName/Local/Latex/texmf-dist/tex/latex/psnfss/courier.sty 
) (/home/users/MyName/Local/Latex/texmf-dist/tex/latex/base/textcomp.sty 
(/home/users/MyName/Local/Latex/texmf-dist/tex/latex/base/ts1enc.def)) 
(/home/users/MyName/texmf/tex/latex/xfrac.sty 
(/home/users/MyName/Local/Latex/texmf-dist/tex/latex/amsmath/amstext.sty 
(/home/users/MyName/Local/Latex/texmf-dist/tex/latex/amsmath/amsgen.sty)) 
 
(/home/users/MyName/Local/Latex/texmf-dist/tex/latex/graphics/graphicx.st 
y 
(/home/users/MyName/Local/Latex/texmf-dist/tex/latex/graphics/graphics.st 
y (/home/users/MyName/Local/Latex/texmf-dist/tex/latex/graphics/trig.sty) 
 
(/home/users/MyName/Local/Latex/texmf-dist/tex/latex/latexconfig/graphics 
.cfg) 
(/home/users/MyName/Local/Latex/texmf-dist/tex/latex/graphics/dvips.def)) 
) (/home/users/MyName/texmf/tex/latex/l3keys2e.sty 
(/home/users/MyName/Local/Latex/texmf-dist/tex/latex/l3kernel/expl3.sty 
(/home/users/MyName/Local/Latex/texmf-dist/tex/latex/l3kernel/expl3-code. 
tex 
(/home/users/MyName/Local/Latex/texmf-dist/tex/latex/etex-pkg/etex.sty)) 
(/home/users/MyName/Local/Latex/texmf-dist/tex/latex/l3kernel/l3unicode-d 
ata.def) 
(/home/users/MyName/Local/Latex/texmf-dist/tex/latex/l3kernel/l3dvips.def 
))) (/home/users/MyName/texmf/tex/latex/xparse.sty) 
(/home/users/MyName/texmf/tex/latex/xtemplate.sty)) 
(/home/users/MyName/Local/Latex/texmf-dist/tex/latex/geometry/geometry.st 
y 
(/home/users/MyName/Local/Latex/texmf-dist/tex/generic/oberdiek/ifpdf.sty 
) 
(/home/users/MyName/Local/Latex/texmf-dist/tex/generic/oberdiek/ifvtex.st 
y) 
(/home/users/MyName/Local/Latex/texmf-dist/tex/generic/ifxetex/ifxetex.st 
y) 
 
Package geometry Warning: Over-specification in `h'-direction. 
    `width' (614.295pt) is ignored. 
 
 
Package geometry Warning: Over-specification in `v'-direction. 
    `height' (794.96999pt) is ignored. 
 
) (/home/users/MyName/Local/Latex/texmf-dist/tex/latex/psfrag/psfrag.sty) 
 
 
! LaTeX Error: Option clash for package graphicx. 
 
See the LaTeX manual or LaTeX Companion for explanation. 
Type  H <return>  for immediate help. 
 ...                                               
 
l.13 \usepackage 
                {color} 
(/home/users/MyName/Local/Latex/texmf-dist/tex/latex/graphics/color.sty 
(/home/users/MyName/Local/Latex/texmf-dist/tex/latex/latexconfig/color.cf 
g) 
(/home/users/MyName/Local/Latex/texmf-dist/tex/latex/graphics/dvipsnam.de 
f)) 
No file tmp0Nr4Ze.aux. 
(/home/users/MyName/Local/Latex/texmf-dist/tex/latex/base/ts1cmr.fd) 
(/home/users/MyName/Local/Latex/texmf-dist/tex/latex/psnfss/ot1pnc.fd) 
*geometry* detected driver: dvips 
<tmp0Nr4Ze.eps> [1] (./tmp0Nr4Ze.aux) ) 
(see the transcript file for additional information) 
Output written on tmp0Nr4Ze.dvi (1 page, 3368 bytes). 
Transcript written on tmp0Nr4Ze.log. 

有人知道如何解决这个问题吗?

编辑

我现在发现,如果我尝试将其保存为 pgf(LaTeX PGF 图)

我收到这个错误

Error processing '\(\displaystyle \sfrac{\tau_{peel}}{\tau_{m}}\)' 
LaTeX Output: 
! Undefined control sequence. 
<argument> ...}\selectfont \(\displaystyle \sfrac  
                                                  {\tau _{peel}}{\tau _{m}}\) 
<*> ...splaystyle \sfrac{\tau_{peel}}{\tau_{m}}\)} 
 
No pages of output. 
Transcript written on texput.log. 

编辑 2:

我有时会遇到这个错误

dvipng warning: No image output from inclusion of raw PostScript GPL Ghostscript 9.05: Unrecoverable error, exit code 1 

所以我更新了 Ghostscript,现在我得到了这个错误:-)

dvipng warning: No image output from inclusion of raw PostScript GPL Ghostscript 9.14: Unrecoverable error, exit code 1 

请您参考如下方法:

在这种情况下,为了输出 ps — 您需要将 dvips 选项传递给 graphicx:

\usepackage[dvips]{graphicx} 

脚本应该是:

import matplotlib 
import matplotlib.pyplot as plt 
 
# Use LaTeX for rendering 
matplotlib.rcParams["text.usetex"] = True 
# load the xfrac package 
matplotlib.rcParams["text.latex.preamble"].append(r'\usepackage[dvips]{graphicx}\usepackage{xfrac}') 
 
fig = plt.figure() 
ax = fig.add_subplot(111) 
ax.plot([0,1],[1,0]) 
ax.text(.5, .5, r'$\sfrac{1}{2}$') 
plt.savefig('111.ps') 

可能 graphicx 由 matplotlib 加载,为了输出 ps matplotlib 使用 dvips 驱动程序,因此该选项必须被传递给 graphicx。

我认为虽然使用您的原始代码输出 pdf 并使用 ghostscript 将其转换为 ps 会更容易。