Enthought Canopy 中的 Matplotlib 字体

标签 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]))

我使用下面的小脚本,它检查字体的存在/不存在。如果不存在,它会下载它。
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 

我错过了什么?

最佳答案

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

首先,我在 Enthought 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”文件/字体,所以我手动下载并将其复制到目录中:
./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

之后,我打开了我的 Canopy 编辑器,打开了一个 iPython 笔记本,写了一些代码,绘制了一些图表,很快,我的字体是对的!

final output

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

关于Enthought Canopy 中的 Matplotlib 字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20206906/

相关文章:

python - 使用 ArtistAnimation 在 matplotlib 中动画化 png

python - 在 matplotlib 中禁用 imshow colorbar 的科学记数法

virtual-machine - 在 Ubuntu 12.04 VirtualBox Guest 上安装 IPython 并从 Windows 主机上的浏览器使用它

python - 如何使用 Python 将字节数组发送到串行端口?

python - 如何将python代码修改为cython代码?

python - 如何从 Pandas Dataframe 创建事件图(如 Github 贡献图)

python - 使用 matplotlib 或 PIL 将 3d numpy 数组绘制为 3d 图像

python - "%matplotlib inline"的目的

python - 为什么我的代码不能在 ipython notebook 上执行?

linux - 在centos linux x86_64上安装Enthought canopy