python修改字体无效,无法在Mac OS X 10.6上的Python的Matplotlib中将字体更改为Helvetica...

本文介绍了一种将Matplotlib默认字体更改为Helvetica的方法,并解决了在使用PDF后端时遇到的问题。通过将.dfont格式的Helvetica字体转换为.ttf格式并放置在Matplotlib字体目录中,成功实现了字体更改。

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

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

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

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.

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?

thanks.

解决方案

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.

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值