禁用matplotlib刻度标签中的usetex

时间:2018-04-11 23:09:33

标签: python matplotlib latex

我正在尝试使用Python(版本3.5.3)和Matplotlib(版本2.0.2)创建一个图形。我希望图中的一个轴用Latex中渲染的方程标记。为了渲染等式,我需要使用amsmath和amsfonts包。

我设法做到这一点的唯一方法是在matplotlib数字中为所有文本全局设置usetex = True。但是,这会导致刻度标签也在Latex中呈现。我可以阻止这种情况,如果是这样,怎么办?

import numpy as np
import matplotlib.pyplot as plt
preamble='\\usepackage{amsmath}\n\\usepackage{amsfonts}'

plt.rc('text',usetex=True)
plt.rc('text.latex',preamble=preamble)

fig,ax=plt.subplots()

all_lambda = np.linspace(-6,2,1000)
ax.plot(all_lambda,np.exp(-np.exp(all_lambda)))
ax.set_xlim([-6,2])
ax.set_ylim([-0.1,1.1])
ax.set_xlabel(r'$\log\lambda$')
ax.set_ylabel(r'$\mathbb{P}(m=0\lvert \lambda)$')
fig.tight_layout()
plt.show()

matplotlib graph

1 个答案:

答案 0 :(得分:0)

也许您想使用MathText而不是乳胶?

plt.rcParams["mathtext.fontset"] = "stixsans"

没有乳胶的完整示例:

import numpy as np
import matplotlib.pyplot as plt

plt.rcParams["axes.labelsize"] = "large"
plt.rcParams["mathtext.fontset"] = "stixsans"


fig,ax=plt.subplots()

all_lambda = np.linspace(-6,2,1000)
ax.plot(all_lambda,np.exp(-np.exp(all_lambda)))
ax.set_xlim([-6,2])
ax.set_ylim([-0.1,1.1])
ax.set_xlabel(r'$\log\lambda$')
ax.set_ylabel(r'$\mathbb{P}(m=0\left| \lambda\right)$')
fig.tight_layout()
plt.show()

enter image description here