我在Canopy中使用matplotlib库,具体的函数是xkcd()。此函数使用特定字体绘制图表。字体为Comic Sans MS,如果不存在,则应下载。
/home/luis/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/font_manager.py:1236: UserWarning: findfont: Font family ['Humor Sans', 'Comic Sans MS'] not found. Falling back to Bitstream Vera Sans (prop.get_family(), self.defaultFamily[fontext]))我使用下面的小脚本,它检查字体的存在/不存在。如果不存在,它会下载它。
import os
import urllib2
if not os.path.exists('Humor-Sans.ttf'):
fhandle = urllib2.urlopen('http://antiyawn.com/uploads/Humor-Sans-1.0.ttf')
open('Humor-Sans.ttf', 'wb').write(fhandle.read())问题是我仍然没有得到正确的字体来显示。如果字体缓存出现问题,我会执行以下操作:
luis@luis-VirtualBox:~$ rm /home/luis/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/fontList.cache获取以下内容:
rm: cannot remove ‘/home/luis/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/fontList.cache’: No such file or directory 我遗漏了什么?
发布于 2013-11-29 08:12:44
经过大量的研究,没有找到任何人可以帮助我回答我的问题,我能够回答我自己的问题。这是我所做的:
首先,我找到了所有字体在虚拟环境中的虚拟环境的matplotlib的确切位置:
luis@luis-VirtualBox:~$ find -iname '*.ttf'将生成一个很长的列表,结果类似于:
./Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf/Vera.ttf
./Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf/VeraMoBI.ttf
./Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf/STIXGeneral.ttf
./Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf/STIXNonUniBol.ttf
./Canopy/appdata/canopy-1.1.0.1371.rh5-x86_64/lib/python2.7/site-packages/canopy/resources/fonts/Inconsolata.ttf我在任何地方都看不到'Humor-Sans-1.0.ttf‘文件/font,所以我手动下载并将其复制到目录中:
./Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf/
尽管如此,图表还是默认使用另一种字体:
Font family ['Humor Sans', 'Comic Sans MS'] not found. Falling back to Bitstream Vera Sans (prop.get_family(), self.defaultFamily[fontext]))然后我注意到我下载的字体是'Humor-Sans-1.0.ttf‘,错误消息是指'Humor Sans’和'Comic Sans‘(没有1.0附录)。因此,我在同一个目录中制作了同一文件的两个副本,并分别将它们命名为“Humor-Sans.ttf”和“Comic-Sans.ttf”。
接下来,我找到了matplotlib fontCache列表在我的虚拟环境中的位置:
luis@luis-VirtualBox:~$ find -iname 'fontList.cache'
./.cache/matplotlib/fontList.cache然后删除缓存:
luis@luis-VirtualBox:~$ rm ./.cache/matplotlib/fontList.cache在那之后,我打开我的树冠编辑器,打开一个iPython笔记本,写了一些代码,画了一些图表,然后,我的字体就对了!

这不是最优雅的解决方案,但它对我很有效。
发布于 2018-11-19 22:19:50
这对我来说很有效,而且我也可以在jupyter笔记本上做一些事情:
只需在python控制台(或jupyter笔记本)中键入以下内容:
matplotlib.font_manager._rebuild()发布于 2014-02-05 04:34:00
我有(不幸的)在Windows环境中工作的要求,并且遇到了同样的问题。对于那些在Windows中工作的人,我想补充的一件事是,重要的不一定是文件的名称,而是字体的标题。
对于我的问题,下载了helvetica.ttf并将其放入目录中
C:\Python27\Lib\site-packages\matplotlib\mpl-data\fonts\ttf但是,由于文件的属性将字体的标题列为"Helvetica-normal“,因此我需要确保指定的是
font.sans-serif : Helvetica-normal在我的matplotlibrc文件中,即使文件名只是"helvetica.ttf“
https://stackoverflow.com/questions/20206906
复制相似问题