如何避免在matplotlib中调用乳胶(输出到pgf) [英] How to avoid calling latex in matplotlib (output to pgf)

查看:249
本文介绍了如何避免在matplotlib中调用乳胶(输出到pgf)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用matplotlib及其pgf后端来生成包含在LaTeX Beamer文档中的图.使用未定义的乳胶命令时,我会遇到麻烦.但是对于我的应用程序,我不需要matplotlib来生成带有乳胶的标签或注释,我只想要正确的pgf输出,我将在我的投影仪文档上调用LaTeX.如果我要在笔记本中运行此代码,则希望在xlabel中有一个带有文字"\ si {\ percent}"的图.

I'm using matplotlib with its pgf backend to generate plots that I include in my LaTeX beamer document. I run into trouble when I use latex commands that are not defined. But for my application, I don't need matplotlib to generate the labels or annotations with latex, I only want a correct pgf output and I will call LaTeX on my beamer document. If I would run this code in a notebook, I would expect just to have a plot with a literal "\si{\percent}" in the xlabel.

在下面的MWE中,当我用注释行(使用\si{\percent})运行它时,matplotlib崩溃并发生了乳胶错误(未知命令si").我不想使用matplotlib创建序言,我只想包含\ si {\ percent}命令的pgf输出...

In the MWE below, when I run it with the commented line (using \si{\percent}), matplotlib crashes with a latex error ('unknown command si'). I don't want to create a preamble with matplotlib, I just want the pgf output containing the \si{\percent} command...

如果我使用双反斜杠,则代码会通过,但是双反斜杠也会出现在pfg输出中,因此,乳胶无法识别该命令(我猜它会看到换行符).

If I use double backslashes, the code passes but the double backslash appears also in the pfg output and hence latex doesn't recognize the command (it sees a newline, I guess).

我不了解plt.rc('text',usetex = False)的值".我以为这会完全禁止调用LaTeX ...

I don't understand the "value" of the plt.rc('text', usetex=False). I thought this would disable calling LaTeX alltogether...

import numpy as np
import matplotlib as mpl
mpl.use('pgf')
from matplotlib import pyplot as plt
from matplotlib import rc
plt.style.use('bmh')
plt.rc('pgf',rcfonts=False)
plt.rc('text', usetex=False)
x = np.linspace(0,100,101)
y = np.cos(x/100)*np.exp(-x/100)
plt.plot(x,y)
#plt.xlabel(r'value (\si{\percent})')
plt.xlabel(r'value (%)')
plt.savefig('test.pgf')

推荐答案

您是否有任何犹豫要添加序言的原因?这样做可以提供一个简单的解决方案.以下对我有用:

Is there any reason why you're hesitant to include a preamble? Doing so makes for an easy solution. The following works for me:

import numpy as np
import matplotlib as mpl

mpl.use('pgf')

from matplotlib import pyplot as plt

pgf_with_latex = {
        'text.usetex': False,
        'pgf.rcfonts': False,
        "pgf.preamble": [
                r"\usepackage{siunitx}"
                ]
}

mpl.rcParams.update(pgf_with_latex)

plt.style.use('bmh')

x = np.linspace(0,100,101)
y = np.cos(x/100)*np.exp(-x/100)
plt.plot(x,y)

plt.xlabel(r'value (\si{\percent})')
plt.savefig('test.pgf')

这篇关于如何避免在matplotlib中调用乳胶(输出到pgf)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆