首页 > 解决方案 > matplotlib pgf 后端与 lualatex - 非数学字体不会改变

问题描述

在生成像 hexbin 这样的“更重”图时,我有时需要lualatex引擎使用 matplotlib 的 pgf 后端,否则会超出 TeX 内存并pdflatex失败。使用lualatex数学字体时,似乎会根据我pgf.preamble的 . 使用pdflatex,字体表现出我想要的样子。请注意,如果我访问www.overleaf.com并使用相同的序言并使用pdflatexor进行编译lualatex,该文档会在数学和正文中显示我想要的字体。

使用 lua 引擎: 在此处输入图像描述

使用pdf引擎: 在此处输入图像描述

代码:

import matplotlib as mpl

eng = 'lua' # issue with lua only   <<<<<<<<
# eng = 'pdf' # no issue here

mpl.use('pgf')
mpl.rc('font', family='serif')
mpl.rcParams.update({
        "pgf.rcfonts"  : False,
        "pgf.texsystem": eng + "latex",
        "pgf.preamble" : '\\usepackage[utf8x]{inputenc}\\usepackage[light]{kpfonts}',
})

import numpy as np
import matplotlib.pyplot as plt

t = np.linspace(0,6,100)
x = np.sin(t)

fig, ax = plt.subplots()
ax.plot(t, x, label='A line')
plt.title('A function, $f(x) = \sin(x)$')
plt.legend()
plt.savefig('example.pdf')

标签: matplotliblatexpgf

解决方案


推荐阅读