首页 > 解决方案 > 如何在 matplotlib 中使用 Nimbus Roman 字体?

问题描述

为了匹配我在 LaTeX 中的文本字体,我想Nimbus Roman No9 L在 matplotlib 中使用。

我检查了我的 LaTeX PDF 和 pdffonts 返回:

name                                 type              encoding         emb sub uni object ID
------------------------------------ ----------------- ---------------- --- --- --- ---------
SRVJHK+NimbusRomNo9L-Regu            Type 1            Custom           yes yes no       6  0
LXAKKR+NimbusSanL-Regu               Type 1            Custom           yes yes no      12  0
ZNRHEK+NimbusRomNo9L-ReguItal        Type 1            Custom           yes yes no      16  0

我认为在 matplotlib 中切换字体会很简单。

我附上了一个最小的例子:

from matplotlib import pyplot as plt

plt.rcParams.update({
    'font.family': 'serif',
    'font.serif': 'Times New Roman',  # 'Nimbus Roman No9 L',
})

plt.bar(['A ($1+1$)', 'B', 'C'], [1, 2, 3], align="center")

plt.savefig('minimal_example.pdf')
plt.show()

当我尝试加载 Nimbus Roman No9 LI 时,总是会收到一条错误消息:

WARNING:matplotlib.font_manager:findfont: Font family ['serif'] not found. Falling back to DejaVu Sans.
WARNING:matplotlib.font_manager:findfont: Generic family 'serif' not found because none of the following families were found: Nimbus Roman No9 L

Times New Roman 加载,但不用于 $$ 部分。

> pdffonts minimal_example.pdf
name                                 type              encoding         emb sub uni object ID
------------------------------------ ----------------- ---------------- --- --- --- ---------
TimesNewRomanPSMT                    Type 3            Custom           yes no  no      21  0
DejaVuSans                           Type 3            Custom           yes no  no      15  0

如何在绘图中使用与文本中相同的字体?

编辑:我能够加载 Nimbus Roman No9 L,它被称为NimbusRomNo9L.

plt.rcParams.update({
    'font.family': 'NimbusRomNo9L'
})

但数学公式仍然使用 DejaVuSans

> pdffonts minimal_example.pdf
name                                 type              encoding         emb sub uni object ID
------------------------------------ ----------------- ---------------- --- --- --- ---------
NimbusRomNo9L-Regu                   Type 3            Custom           yes no  no      14  0
DejaVuSans                           Type 3            Custom           yes no  no      28  0

标签: pythonmatplotlibfontslatex

解决方案


推荐阅读