我正在尝试在 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 许可协议
如果在安装 matplotlib 后添加新字体,请尝试删除字体缓存。 Matplotlib 将不得不重建缓存,从而添加新字体。
它可能位于
~/.matplotlib/fontList.cache
或~/.cache/matplotlib/fontList.json
下。