Enthought Canopy 中的 Matplotlib 字体

13

我正在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 

我错过了什么?


1
这是世界告诉你不要使用Comic Sans字体 ;) 你的字体缓存应该在类似./.cache/matplotlib/fontList.cache的地方,而不是安装路径中。你可能正在删除系统级别的fontList.cache,它永远不会被重新生成。 - tacaswell
tcaswell,非常感谢您的反馈。我尝试从您指定的位置删除fontList.cache,但仍无法使matplotlib显示正确的字体。 - Luis Miguel
3个回答

23

经过大量研究后,我没有找到任何可以帮助我回答问题的人,但最终我自己解决了这个问题。下面是我所做的:

首先,在 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字体缓存列表在我的虚拟环境中的位置:
luis@luis-VirtualBox:~$ find -iname 'fontList.cache'
./.cache/matplotlib/fontList.cache

然后清除缓存:

luis@luis-VirtualBox:~$ rm ./.cache/matplotlib/fontList.cache

之后,我打开了我的Canopy编辑器,打开了一个iPython笔记本,写了一些代码,绘制了一些图形,然后瞧,我的字体就对了!

最终输出

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


1
谢谢!这个以及这里的提示:https://dev59.com/L2Ei5IYBdhLWcg3wPKax 使我成功了! - JJC
感谢 @SanyamJain 分享这个信息。 - Luis Miguel

3
这种方法适合我,并且有一个额外的好处,就是我可以在jupyter笔记本内完成:

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

matplotlib.font_manager._rebuild()

2

很不幸,我需要在Windows环境下工作,并遇到了同样的问题。对于那些在Windows中工作的人,我要补充的一件事是,重要的不一定是文件名,而是字体的名称。

为了解决我的问题,我下载了Helvetica.ttf字体并将其放入目录中。

C:\Python27\Lib\site-packages\matplotlib\mpl-data\fonts\ttf

然而,由于文件属性将字体标题列为“Helvetica-normal”,因此我需要确保我指定了该字体。
font.sans-serif      : Helvetica-normal

在我的matplotlibrc文件中,即使文件的名称只是"helvetica.ttf"。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接