Enthought Canopy中的Matplotlib字体 [英] Matplotlib fonts in Enthought Canopy

查看:429
本文介绍了Enthought Canopy中的Matplotlib字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Canopy中使用matplotlib库,具体功能是xkcd()。此功能使用特定字体绘制图表。字体是Comic Sans MS,如果不存在,应该下载。

I am using the matplotlib library inside Canopy, and the specific function is xkcd(). This function uses a specific font to plot charts. The font is Comic Sans MS, which if not present, should be downloaded.

/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]))

我使用下面的小脚本,它检查字体的存在/不存在。如果不存在,则下载它。

I use the small script below, which checks the presence/absence of the font. If not present, it downloads it.

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())

问题是我仍然没有得到正确的字体来显示。如果字体缓存出现问题,我会执行以下操作:

The problem is that I still don't get the right font to display. In case there is a problem with the font cache, I do the following:

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 

什么我错过了吗?

推荐答案

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

After a lot of research, and not finding anybody who could help me with my question, I was able to answer my own question. This is what I did:

首先,我在Enthought Canopy的虚拟环境中找到matplotlib中所有字体的确切位置:

First, I found exactly where all the fonts are in within matplotlib in the virtual environment of Enthought Canopy:

luis@luis-VirtualBox:~$ find -iname '*.ttf'

生成一个长列表,其结果与此类似:

A long list is generated, with results similar to this:

./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'文件/字体,所以我手动操作下载并将其复制到目录:

I could not see the 'Humor-Sans-1.0.ttf' file/font anywhere, so I manually downloaded and copied it to the directory:

./ Enthought / Canopy_64bit / User / lib / python2.7 / site-packages / matplotlib / mpl -data / fonts / ttf /

但是,图表默认为另一种字体:

Still, the chart was defaulting to another font:

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。

Then I noticed that the font I had downloaded was 'Humor-Sans-1.0.ttf' and the error messages was referring to 'Humor Sans' and 'Comic Sans' (without the 1.0 appendix). So I made two copies of the same file, inside the same directory and called them 'Humor-Sans.ttf' and 'Comic-Sans.ttf' respectively.

接下来,我发现matplotlib fontCache列表位于我的虚拟环境中:

Next, I found where the matplotlib fontCache list resides within my virtual environment:

luis@luis-VirtualBox:~$ find -iname 'fontList.cache'
./.cache/matplotlib/fontList.cache

然后删除缓存:

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

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

After that, I opened my Canopy Editor, opened an iPython notebook, wrote some code, plotted some graphs, and presto, my fonts were right!

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

Not the most elegant solution, but it worked for me.

这篇关于Enthought Canopy中的Matplotlib字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆