Lui*_*uel 11 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 found. Falling back to Bitstream Vera Sans (prop.get_family(), self.defaultFamily[fontext]))
Run Code Online (Sandbox Code Playgroud)
我使用下面的小脚本,它检查字体的存在/不存在.如果不存在,则下载它.
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())
Run Code Online (Sandbox Code Playgroud)
问题是我仍然没有得到正确的字体来显示.如果字体缓存出现问题,我会执行以下操作:
luis@luis-VirtualBox:~$ rm /home/luis/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/fontList.cache
Run Code Online (Sandbox Code Playgroud)
获得以下内容:
rm: cannot remove ‘/home/luis/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/fontList.cache’: No such file or directory
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
Lui*_*uel 19
经过大量的研究,没有找到任何可以帮助我解决问题的人,我能够回答我自己的问题.这就是我做的:
首先,我在Enthought Canopy的虚拟环境中找到matplotlib中所有字体的确切位置:
luis@luis-VirtualBox:~$ find -iname '*.ttf'
Run Code Online (Sandbox Code Playgroud)
生成一个长列表,结果类似于:
./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
Run Code Online (Sandbox Code Playgroud)
我无法在任何地方看到'Humor-Sans-1.0.ttf'文件/字体,所以我手动下载并将其复制到目录:
./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]))
Run Code Online (Sandbox Code Playgroud)
然后我注意到我下载的字体是'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
Run Code Online (Sandbox Code Playgroud)
然后删除缓存:
luis@luis-VirtualBox:~$ rm ./.cache/matplotlib/fontList.cache
Run Code Online (Sandbox Code Playgroud)
之后,我打开了我的Canopy编辑器,打开了一个iPython笔记本,编写了一些代码,绘制了一些图表,并且我的字体是正确的!
不是最优雅的解决方案,但它对我有用.