Python Matplotlib可以';找不到字体

Python Matplotlib可以';找不到字体,python,python-3.x,matplotlib,fonts,Python,Python 3.x,Matplotlib,Fonts,我试图在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_mana

我试图在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
我安装了幽默软件。我用
fc list | grep Humor
检查了它。它也可以在其他程序中使用,如Libre Office。我还安装了staypuft。这还不够吗

上面的代码相同,但没有plt.xkcd()位,可以完美地工作


plt.show()的替代方法,如pylab.savefig()对xkcd代码也不起作用,但如果不使用xkcd,则同一代码不会有任何问题。

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


它可能位于Mac用户的
~/.matplotlib/fontList.cache
~/.cache/matplotlib/fontList.json

下:尝试在python中运行此命令:(或在.py文件之前)


以防有人想为他们的图表选择自定义字体。您可以手动设置图表标签、标题、图例或记号标签的字体。下面的代码演示如何为图表设置自定义字体。你提到的错误可以消失

import matplotlib.font_manager as fm
import matplotlib.pyplot as plt

font_path = '/System/Library/Fonts/PingFang.ttc'  # the location of the font file
my_font = fm.FontProperties(fname=font_path)  # get the font based on the font_path

fig, ax = plt.subplots()

ax.bar(x, y, color='green')
ax.set_xlabel(u'Some text', fontproperties=my_font)
ax.set_ylabel(u'Some text', fontproperties=my_font)
ax.set_title(u'title', fontproperties=my_font)
for label in ax.get_xticklabels():
    label.set_fontproperties(my_font)

我搜索了fontList并在~/.cache/matplotlib/fontList.py3k.cache中找到了它。删除它会使上述代码再次工作。是的,matplotlib配置文件的位置由操作系统定义。在Windows上,您需要查看%HOMEPATH%\.matplotlib。有一个文件fontList.py3k.cache。删除它。如果您使用的是jupyter笔记本电脑,则必须重新启动jupyter,然后才能拾取新字体并重新创建缓存。您可以通过运行
import matplotlib as mpl;打印(mpl.get\u cachedir())
这似乎是当前版本matplotlib的有效解决方案。我找不到
fontList
缓存。至少在
matplotlib
v3.3.2
matplotlib.font\u管理器在导入
matplotlib
时是未知的,因此需要执行
导入matplotlib.font\u管理器;matplotlib.font\u manager.\u rebuild()
对我很有用,非常感谢。
import matplotlib.font_manager as fm
import matplotlib.pyplot as plt

font_path = '/System/Library/Fonts/PingFang.ttc'  # the location of the font file
my_font = fm.FontProperties(fname=font_path)  # get the font based on the font_path

fig, ax = plt.subplots()

ax.bar(x, y, color='green')
ax.set_xlabel(u'Some text', fontproperties=my_font)
ax.set_ylabel(u'Some text', fontproperties=my_font)
ax.set_title(u'title', fontproperties=my_font)
for label in ax.get_xticklabels():
    label.set_fontproperties(my_font)