【问题标题】:Matplotlib fonts in Enthought CanopyEnthought Canopy 中的 Matplotlib 字体
【发布时间】:2013-12-10 23:53:48
【问题描述】:

我使用的是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 

我错过了什么?

【问题讨论】:

  • 全世界都在告诉你不要使用comic sans ;) 你的字体缓存应该在像./.cache/matplotlib/fontList.cache 这样不在安装路径中的地方。您可能正在删除永远不会重新生成的系统安装级别 fontList.cache
  • tcaswell,非常感谢您的反馈。我尝试从您指定的位置删除 fontList.cache,但我仍然没有让 matplotlib 显示正确的字体

标签: matplotlib ipython canopy


【解决方案1】:

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

首先,我在 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 笔记本,写了一些代码,绘制了一些图表,然后,我的字体是正确的!

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

【讨论】:

【解决方案2】:

这对我有用,而且我也可以在 jupyter notebook 中做一些事情:

只需在 python 控制台(或您的 jupyter notebook)中输入以下内容:

matplotlib.font_manager._rebuild()

【讨论】:

    【解决方案3】:

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

    针对我的问题,下载了 helvetica.ttf 放到目录下

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

    但是,由于文件的属性将字体的标题列为“Helvetica-normal”,因此我需要确保我指定了

    font.sans-serif      : Helvetica-normal
    

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

    【讨论】:

      猜你喜欢
      • 2013-11-18
      • 1970-01-01
      • 1970-01-01
      • 2013-04-15
      • 1970-01-01
      • 2013-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多