Matplotlib找不到字体 [英] Matplotlib can't find font
问题描述
我正在尝试在Python 3下使用matplotlib(版本1.4.2)绘制xkcd样式的图.
I am trying to draw an xkcd-style plot with matplotlib (ver. 1.4.2) under Python 3.
当我尝试运行时:
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()
它打开一个没有任何图像的空窗口,我得到了错误:
It opens an empty window without any image and I get the error:
/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.还不够吗?
I have Humor Sans installed. I checked it with fc-list | grep Humor
. It can also be used within other programs, like Libre Office. I also have staypuft installed. Isn't that enough?
上面相同的代码但没有plt.xkcd()位可以正常工作.
The same code above but without the plt.xkcd() bit works flawlessly.
对于plt.show()的替代方法,例如pylab.savefig()对于xkcd代码都不起作用,但是如果不使用xkcd,相同代码也不会有任何问题.
An alternative to plt.show(), like pylab.savefig() won't work either for the xkcd code, but doesn't have any problem with the same code without using xkcd.
推荐答案
如果在安装matplotlib之后添加新字体,请尝试删除字体缓存. Matplotlib将必须重建缓存,从而添加新字体.
If you add a new font after installing matplotlib then try to remove the font cache. Matplotlib will have to rebuild the cache, thereby adding the new font.
它可能位于~/.matplotlib/fontList.cache
或~/.cache/matplotlib/fontList.json
下.
这篇关于Matplotlib找不到字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!