Matplotlib 在我的 Linux 机器上找不到安装的字体

新手上路,请多包涵

我正在尝试在 Python 3 下使用 matplotlib(版本 1.4.2)绘制一个 xkcd 风格的图。

当我尝试运行时:

 import matplotlib.pyplot as plt
plt.xkcd()
plt.plot([1,2,3,4], [1,4,9,16], 'bo')
plt.axis([0, 6, 0, 20])
plt.show()

它打开一个没有任何图像的空窗口,我得到错误:

 /usr/lib/python3/dist-packages/matplotlib/font_manager.py:1279: UserWarning: findfont: Font family ['Humor Sans', 'Comic Sans MS', 'StayPuft'] not found. Falling back to Bitstream Vera Sans
  (prop.get_family(), self.defaultFamily[fontext]))
/usr/lib/python3/dist-packages/matplotlib/font_manager.py:1289: UserWarning: findfont: Could not match :family=Bitstream Vera Sans:style=normal:variant=normal:weight=normal:stretch=normal:size=medium. Returning /usr/share/matplotlib/mpl-data/fonts/ttf/STIXSizOneSymReg.ttf
  UserWarning) Exception in Tkinter callback

我安装了 Humor Sans。我用 fc-list | grep Humor 检查了它。它还可以在其他程序中使用,例如 Libre Office。我也安装了 staypuft。这还不够吗?

上面相同的代码但没有 plt.xkcd() 位可以完美运行。

plt.show() 的替代方案,如 pylab.savefig() 也不适用于 xkcd 代码,但在不使用 xkcd 的情况下,相同代码不会有任何问题。

原文由 Pierre B 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 835
2 个回答

如果在安装 matplotlib 后添加新字体,请尝试删除字体缓存。 Matplotlib 将不得不重建缓存,从而添加新字体。

它可能位于 ~/.matplotlib/fontList.cache~/.cache/matplotlib/fontList.json 下。

原文由 Serenity 发布,翻译遵循 CC BY-SA 4.0 许可协议

对于 Mac 用户:尝试在 python 中运行此命令:(或在 .py 文件之前)

 import matplotlib

matplotlib.font_manager._rebuild()

原文由 Zhihui Shao 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题