Enthow Corporation中的Matplotlib字体

Enthow Corporation中的Matplotlib字体,matplotlib,ipython,canopy,Matplotlib,Ipython,Canopy,我在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 fo

我在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 
获取以下信息:

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 

我遗漏了什么?

经过大量的研究,没有找到任何人可以帮助我解决我的问题,我能够回答我自己的问题。这就是我所做的:

首先,我在Enthught Canopy的虚拟环境中找到了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,因此我手动下载并将其复制到目录:

/enthund/Canopy\u 64bit/User/lib/python2.7/site packages/matplotlib/mpl data/font/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附录)。所以我在同一个目录下复制了同一个文件的两个副本,分别称它们为“幽默Sans.ttf”和“漫画Sans.ttf”

接下来,我找到了matplotlib fontCache列表在虚拟环境中的位置:

luis@luis-VirtualBox:~$ find -iname 'fontList.cache'
./.cache/matplotlib/fontList.cache
然后删除缓存:

luis@luis-VirtualBox:~$ rm ./.cache/matplotlib/fontList.cache
在那之后,我打开了我的Canopy编辑器,打开了一个iPython笔记本,写了一些代码,画了一些图表,还有,我的字体是对的

这不是最优雅的解决方案,但它对我很有效。

我(不幸)需要在Windows环境中工作,遇到了同样的问题。对于那些在Windows中工作的人,我想补充的一点是,重要的不一定是文件名,而是字体的标题

对于我的问题,下载helvetica.ttf并将其放入目录

C:\Python27\Lib\site-packages\matplotlib\mpl-data\fonts\ttf
但是,由于文件的属性将字体的标题列为“Helvetica normal”,我需要确保指定

font.sans-serif      : Helvetica-normal

在我的matplotlibrc文件中,尽管文件名只是“helvetica.ttf”

这对我来说很有效,而且我还可以在jupyter笔记本中做一些事情:

只需在python控制台(或您的jupyter笔记本)中键入以下内容:


世界告诉你不要使用漫画书;)字体缓存应该位于
/.cache/matplotlib/fontList.cache
之类的位置,而不在安装路径中。您可能正在删除永远不会重新生成的系统安装级别
fontList.cache
。谢谢您的反馈。我尝试从您指定的位置删除fontList.cache,但仍无法让matplotlib显示正确的fontList.cache谢谢!这个,连同这里的提示:救了我!感谢您在SanyamJain分享这些信息