Ubuntu 中 matplotlibrc 中导入 Open Sans 字体失败

ben*_*nzl 5 python ubuntu fonts matplotlib

我尝试导入Open Sans字体:

import matplotlib as mpl
#update matplotlibrc
mpl.rcParams['font.family'] = 'Open Sans'

#testplot
import matplotlib.pyplot as plt
plt.plot(range(10))
plt.title('Everything is crazy!!!', size=32)
plt.show()
Run Code Online (Sandbox Code Playgroud)

如果我想绘图,matplotlib 找不到字体,给出如下错误消息:

/usr/lib/pymodules/python2.7/matplotlib/font_manager.py:1236: UserWarning: findfont: Font family ['Open Sans'] not found. Falling back to Bitstream Vera Sans
      (prop.get_family(), self.defaultFamily[fontext]))

/usr/lib/pymodules/python2.7/matplotlib/font_manager.py:1246: UserWarning: findfont: Could not match :family=Bitstream Vera Sans:style=normal:variant=normal:weight=light:stretch=normal:size=medium. Returning /usr/share/matplotlib/mpl-data/fonts/ttf/cmb10.ttf
      UserWarning)
Run Code Online (Sandbox Code Playgroud)

该字体安装在 Ubuntu 中,所有 OpenSans-*.ttf 文件也被复制到 matplotlib 文件夹(Ubuntu 中的 /usr/share/matplotlib/mpl-data/fonts/ttf/ )。知道如何使用 matplotlibrc 中的字体吗?

小智 3

好吧,这是一个老问题,但我刚刚安装了 Open Sans 以便在 Matplotlib 中使用:

OP 遵循了正确的步骤,除了需要强制重建 Matplotlib 字体缓存。最简单的方法是:

from matplotlib import font_manager
font_manager._rebuild()
Run Code Online (Sandbox Code Playgroud)

请注意,在 Linux 上,以及上面讨论的字体路径,Matplotlib 似乎也在 ~/.fonts 中搜索,因此您可以在那里安装字体。