python - 如何在 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
解决方案
推荐阅读
- javascript - Discord JS:我将如何创建打开/关闭命令?
- mysql - 如何使用 GROUP_CONCAT 获取行 ID 的数据?
- python-3.x - 为什么当我绘制指数函数时绘图是线性的
- html - 根据横向或纵向照片切换 .carousel 高度和宽度
- magento - 如何在 XAMP 中修复 magento 2.3.x 中的管理空白页问题
- python - 在 Opencv Python 中创建多个轨迹栏
- nativescript - 不处理依赖于大小的文件
- javascript - 为什么“带伞”是正确答案?
- ios - bt601 有 iOS Metal 值吗?
- php - PHP检测PNG8或PNG24