Python Matplotlib-使用latex渲染文本时无法将图形另存为eps文件

Python Matplotlib-使用latex渲染文本时无法将图形另存为eps文件,python,matplotlib,Python,Matplotlib,我正在使用latex在matplotlib中渲染文本,但如果任何文本中有latex环境,我无法保存该图形 简化代码: import matplotlib as mpl mpl.use('PS') import matplotlib.pyplot as plt mpl.rcParams['text.usetex'] = True s = (r"\begin{description}" r"\item[someitem:] blah blah blah blah blah \\"

我正在使用latex在matplotlib中渲染文本,但如果任何文本中有latex环境,我无法保存该图形

简化代码:

import matplotlib as mpl
mpl.use('PS')
import matplotlib.pyplot as plt
mpl.rcParams['text.usetex'] = True

s = (r"\begin{description}"
     r"\item[someitem:] blah blah blah blah blah \\"
     r"Some more text that I want to add to my plot"
     r"\item[anotheritem:] This is even more text"
     r"\end{description}")
plt.text(0,0,s)
plt.savefig('myfig.eps')
当我试图保存它时,会出现一个通用的latex错误

!!LaTeX错误:出现问题--可能是缺少\项

有关说明,请参阅LaTeX手册或LaTeX配套手册。 键入H以获得即时帮助

然而,我认为我的乳胶没有任何问题。我试着用上面的代码片段编译一个latex文档,效果很好

有趣的是,如果我使用ipython/jupyter notebook的交互式后端,则会呈现绘图,但我仍然无法将图形保存为ps或eps文件。如果我删除
\begin{…},它也可以工作\结束{…}

如果我在文本中使用latex环境,如何将图形保存为ps/eps文件?

您必须在。您可以将文本包装在
密码框中

import matplotlib as mpl
mpl.use('PS')
import matplotlib.pyplot as plt
mpl.rcParams['text.usetex'] = True

s = (r"\parbox{4in}{"
     r"\begin{description}"
     r"\item[someitem:] blah blah blah blah blah \\"
     r"Some more text that I want to add to my plot"
     r"\item[anotheritem:] This is even more text"
     r"\end{description}"
     r"}"
    )

plt.text(0,0,s)
plt.savefig('myfig.eps')

尝试使用
plt.savefig('myfig.eps',format='eps',dpi=5000)
@Bazingaa输出格式已经从文件名的扩展推导出来,我认为更改dpi不会有任何效果。不管怎样,我试过了,在编译您的TeX文件时收到了相同的错误消息,您确定在编译器设置中的某个地方使用了
ps
ps2pdf
选项吗?不,我不这么认为。但我不明白这与python有什么关系?是否有一些rc选项,如
text.texarguments
?另外,正如我提到的,如果我不使用任何乳胶环境,它是有效的。嗯,我误解了你的问题。我也犯了同样的错误。另存为
.pdf
很好,但这不是您感兴趣的,谢谢。几个小时前,我无意中发现了tex.exchange的答案,但实际上没有时间写答案。