matplotlib中的LaTeX标签不一致

时间:2019-05-08 12:45:47

标签: python matplotlib latex

我尝试在matplotlib图中使用LaTeX标签,但是它们的编译不一致。在下面的屏幕截图中,您不仅可以看到轴的标签,而且还不能在LaTeX中渲染所有的x和y标记。

Python plot with latex labels

以上情节的MWE由

给出
#!/usr/bin/python3

import matplotlib.pyplot as plt

fig = plt.figure()
axes = fig.add_axes([0.1, 0.1, 0.8, 0.8])

# FONT
plt.rc('text', usetex=True)
plt.rc('font', family='serif')

plt.xlabel(r'$y$', fontsize=18)
plt.ylabel(r'$x$',fontsize=18)

# PLOT
axes.plot([1,2,3], [1,4,9], label=r'$x^2$')

# LEGEND
axes.legend(numpoints=1, loc=1, prop={'size':15})

plt.show()

我在同时安装了TeX Live 2018的Debian系统和macOS上尝试了此操作。

如何使LaTeX始终呈现所有标签?

(注意:最后,我想使用Fourier作为数学字体,并使用ebgaramond作为文本字体)

1 个答案:

答案 0 :(得分:1)

我和你做事的顺序有关。您将在创建图形之前在 之前指定rc参数:

import matplotlib.pyplot as plt

plt.rc('text', usetex=True)
plt.rc('font', family='serif')

fig, ax = plt.subplots()

ax.set_xlabel(r'$y$', fontsize=18)
ax.set_ylabel(r'$x$', fontsize=18)

ax.plot([1,2,3], [1,4,9], label=r'$x^2$')

ax.legend(numpoints=1, loc=1, prop={'size':15})

plt.show()

这符合您的意图

enter image description here

请注意,由于您的轴太小,无法显示标签,因此我更改了图形创建。