上的Python的Matplotlib中将字体更改为Helvet

上的Python的Matplotlib中将字体更改为Helvet

本文介绍了无法在Mac OS X 10.6上的Python的Matplotlib中将字体更改为Helvetica的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将matplotlib字体更改为helvetica,我想在PDF图中使用它.我尝试以下方法:

I am trying to change the matplotlib font to helvetica, which I'd like to use in a PDF plot. I try the following:

import matplotlib
matplotlib.use('PDF')
import matplotlib.pylab as plt
from matplotlib import rc
plt.rcParams['ps.useafm'] = True
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
plt.rcParams['pdf.fonttype'] = 42

这不起作用-当我使用--verbose-debug运行代码时,出现错误:

This does not work -- when I run my code with --verbose-debug, I get the error:

backend WXAgg version 2.8.10.1
/Library/Frameworks/Python.framework/Versions/6.2/lib/python2.6/site-packages/matplotlib/__init__.py:833: UserWarning:  This call to matplotlib.use() has no effect
because the the backend has already been chosen;
matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.
findfont: Could not match :family=sans-serif:style=normal:variant=normal:weight=normal:stretch=normal:size=medium. Returning /Library/Frameworks/Python.framework/Versions/6.2/lib/python2.6/site-packages/matplotlib/mpl-data/fonts/ttf/Vera.ttf
Assigning font /F1 = /Library/Frameworks/Python.framework/Versions/6.2/lib/python2.6/site-packages/matplotlib/mpl-data/fonts/ttf/Vera.ttf
Embedding font /Library/Frameworks/Python.framework/Versions/6.2/lib/python2.6/site-packages/matplotlib/mpl-data/fonts/ttf/Vera.ttf
Writing TrueType font

因此,显然它找不到Helvetica.我不知道为什么.我在mpl-data的afm目录中有Helvetica,当matplotlib启动时,它会读取并输出:

So apparently it cannot find Helvetica. I am not sure why. I have Helvetica in the afm directory of mpl-data, and when matplotlib initiates it reads it and outputs:

createFontDict: /Library/Frameworks/Python.framework/Versions/6.2/lib/python2.6/site-packages/matplotlib/mpl-data/fonts/afm/Helvetica.afm

我是否还需要特殊的.ttf Helvetica字体?如果是这样,我该怎么办?我知道我的系统上装有Helvetica,因为我在Illustrator和许多其他程序中都看到了.

Do I need a special .ttf Helvetica font in addition? If so, how can I get it? I know I have Helvetica on my system since I see it in Illustrator and many other programs.

我正在使用如下的Enthought Python发行版:

I am using Enthought Python distribution as follows:

$ python
Enthought Python Distribution -- http://www.enthought.com
Version: 6.2-2 (32-bit)

Python 2.6.5 |EPD 6.2-2 (32-bit)| (r265:79063, May 28 2010, 15:13:03)
[GCC 4.0.1 (Apple Inc. build 5488)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>> matplotlib.__version__
'0.99.3'

有什么想法可以解决此问题吗?

Any ideas how this can be fixed?

谢谢.

推荐答案

解决方案是使用fondu将.dfont Helvetica字体从Mac OS X转换为.ttf,然后将其放置在mpl-data/fonts目录中Matplotlib查找.解决了这个问题.

The solution is to use fondu to convert the .dfont Helvetica font from Mac OS X into .ttf, and then place that in the mpl-data/fonts directory that Matplotlib looks in. That solved the issue.

这篇关于无法在Mac OS X 10.6上的Python的Matplotlib中将字体更改为Helvetica的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 10:39