无法在Mac OS X 10.6的Python中将Matplotlib字体更改为Helvetica

16 投票
3 回答
23740 浏览
提问于 2025-04-16 00:48

我想把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'

有没有什么办法可以解决这个问题?

谢谢。

3 个回答

0

如果这对谁有帮助,我写了一个脚本,可以自动将自定义文件夹中的.ttf字体添加到mpl-data中。你只需要把你的.ttf文件放在一个文件夹里,然后运行这个脚本,就可以把它们移动过去。

#!/usr/bin/env python3
# Imports
import os
import re
import shutil
from glob import glob
from matplotlib import matplotlib_fname
from matplotlib import get_cachedir

# Copy files over
_dir_data = re.sub('/matplotlibrc$', '', matplotlib_fname())
dir_source = '<your-font-directory-here>'
dir_dest = f'{_dir_data}/fonts/ttf'
# print(f'Transfering .ttf and .otf files from {dir_source} to {dir_dest}.')
for file in glob(f'{dir_source}/*.[ot]tf'):
    if not os.path.exists(f'{dir_dest}/{os.path.basename(file)}'):
        print(f'Adding font "{os.path.basename(file)}".')
        shutil.copy(file, dir_dest)

# Delete cache
dir_cache = get_cachedir()
for file in glob(f'{dir_cache}/*.cache') + glob(f'{dir_cache}/font*'):
    if not os.path.isdir(file): # don't dump the tex.cache folder... because dunno why
        os.remove(file)
        print(f'Deleted font cache {file}.')

这个内容最初出现在这个stackoverflow帖子中。

20

这是一个关于如何在OS X 10.11 El Capitan上设置Python 3使用Helvetica字体的详细步骤(参考了这篇文章)。

  1. 首先,安装一个叫做fondu的工具: brew install fondu
  2. 接下来,找出matplotlib的位置:

    python3 -c "import matplotlib ; print(matplotlib.matplotlib_fname())" 
    

    对我来说,它的位置是/usr/local/lib/python3.5/site-packages/matplotlib/mpl-data/matplotlibrc

  3. 然后,复制一份Helvetica字体:

    mkdir ~/Desktop/font_copies
    cp /System/Library/Fonts/Helvetica.dfont ~/Desktop/font_copies
    
  4. 把我们刚刚复制的Helvetica字体从dfont格式转换成ttf格式:

    cd /usr/local/lib/python3.5/site-packages/matplotlib/mpl-data/fonts/ttf/
    fondu -show ~/Desktop/font_copies/Helvetica.dfont
    
  5. 接着,删除字体缓存:rm ~/.matplotlib/fontList.py3k.cache

完成了!现在你可以使用Helvetica字体了:

import matplotlib.pyplot as plt
plt.rc('font', family='Helvetica')
18

解决办法是使用 fondu 工具,把 Mac OS X 上的 .dfont 格式的 Helvetica 字体转换成 .ttf 格式,然后把这个 .ttf 字体放到 Matplotlib 查找的 mpl-data/fonts 文件夹里。这样问题就解决了。

撰写回答