在Matplotlib

时间:2018-05-25 10:16:15

标签: python matplotlib latex

我有以下代码来创建如下所示的情节:

import matplotlib.pyplot as plt

x = [...]
y = [...]
fig = plt.figure()
ax = fig.gca()
ax.set_xlabel(r'$\mathsf{Concentration [mol/m^{3}]}$')
ax.set_ylabel(r'$\mathsf{Diffusion Coefficient [m^2/s]}$')
plt.semilogy(x, y)
plt.grid(True, which="both")
plt.autoscale()
plt.savefig("coeff.pdf")
plt.show()

完全可以将LaTeX数学符号用于轴标签。不幸的是,刻度线/网格线上的数字有另一种字体:

Plot with two different fonts

如何确保轴标签和数字具有相同的字体和样式?

1 个答案:

答案 0 :(得分:1)

您需要将mathtext字体设置为常规字体:

from matplotlib import rcParams
rcParams['mathtext.default'] = 'regular'

这适用于使用mathtext进行方程式渲染。如果你使用Latex,你应该看看here