我正在尝试将 matplotlib 字体更改为 helvetica,我想在 PDF 图中使用它。我尝试以下操作:
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 运行代码时,出现错误:
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 启动时,它会读取它并输出:
createFontDict: /Library/Frameworks/Python.framework/Versions/6.2/lib/python2.6/site-packages/matplotlib/mpl-data/fonts/afm/Helvetica.afm
我还需要特殊的 .ttf Helvetica 字体吗?如果是这样,我怎样才能得到它?我知道我的系统上有 Helvetica,因为我在 Illustrator 和许多其他程序中看到它。
我正在使用 Enthought Python 发行版,如下所示:
$ 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'
有什么想法可以解决这个问题吗?
thanks.