在使用 TeX (MikTeX 2.9) 渲染文本和应用列表中的字体“Times”时,Python (WinPython-64bit-3.6.5.0Qt5)/MATPLOTLIB(版本 2.2.2)出现问题标准字体(参见 Customizing matplotlib )
在下面的最小示例中,我收到以下错误消息:
File "C:\WinPython-64bit-3.6.5.0Qt5\python-3.6.5.amd64\lib\site-packages\matplotlib\dviread.py", line 471, in _fnt_def_real
raise error_class("missing font metrics file: %s" % fontname)
FileNotFoundError: missing font metrics file: rsfs10
这里是一个重现问题的例子:
import numpy as np
import matplotlib.pyplot as plt
# Example data
t = np.arange(0.0, 1.0 + 0.01, 0.01)
s = np.cos(4 * np.pi * t) + 2
from matplotlib import rc
rc('font',**{'family':'serif','serif':['Times']})
rc('text', usetex=True)
plt.plot(t, s)
plt.xlabel(r'\textbf{time} (s)')
plt.ylabel(r'\textit{voltage} (mV)',fontsize=16)
plt.title(r"\TeX\ is Number "
r"$\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!",
fontsize=16, color='gray')
# Make room for the ridiculously large title.
plt.subplots_adjust(top=0.8)
plt.savefig('tex_demo')
plt.show()
看起来 python 无法访问 TeX 字体,例如位于我的 MikTeX 安装文件夹中的 rsfs10 C:\Program Files\MiKTeX 2.9\fonts\source\public\rsfs
如果我不指定字体名称,它使用标准衬线字体(它看起来像计算机现代衬线)。
from matplotlib import rc
rc("pdf", fonttype=3)
rc('font',**{'family':'serif'})
rc('text', usetex=True)
我必须添加以下内容:在下面的最小示例中,我首先收到一条警告消息(我没有在我的完整源代码中收到,它可能与主要问题无关):
C:\WinPython-64bit-3.6.5.0Qt5\python-3.6.5.amd64\lib\site-packages\matplotlib\font_manager.py:1328: UserWarning: findfont: Font family ['serif'] not found. Falling back to DejaVu Sans (prop.get_family(), self.defaultFamily[fontext]))
我已经尝试过这里描述的 Windows 解决方案:Matplotlib cannot find basic fonts但它无助于删除警告。如果我没有将 Times 指定为字体,也不会引发警告。
请您参考如下方法:
我在 matplotlib 中遇到了完全相同的问题。虽然安装 texlive 是一种方法, 我不想同时重新安装另一个 latex 发行版,尤其是因为我已经花了一些时间来设置所有内容并使用其他外部程序运行。
我在 C:\Program Files\MiKTeX\miktex\bin\x64 文件夹中做了一些搜索,它看起来像在运行:
miktex-maketfm.exe rsfs10
成功了。它构建了 rsfs10.tfm 并将其放在 C:\Users\XXX\AppData\Local\MiKTeX\fonts\tfm\public\rsfs 文件夹中。
此解决方案适用于 Miktex 20.10。一旦我运行了这个,matplotlib 就没有问题了。请注意,这篇文章是我的答案的副本 here .
