2010-07-04 129 views
13

我正在尝试将matplotlib字体更改为helvetica,我想在PDF图中使用它。我尝试以下方法:无法在Mac OS X上使用Python在Matplotlib中更改Helvetica的字体10.6

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调试代码,我得到的错误:

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-数据的AFM目录,并在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' 

任何想法,这可怎么固定?

谢谢。

回答

15

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

+3

感谢您提出问题并回答问题 - 我确信这会对我有所帮助,而这正是StackOverflow的创建目的。 – ShreevatsaR 2010-07-04 22:18:44

+0

你能提供更多的细节吗? Max Malysh的分步解决方案就像一个魅力! – meduz 2016-04-21 10:55:32

5

我在安装Helvetica时仍遇到问题。我用fondu上.dfont文件,现在有Helvetica.ttf在mpl-data/fonts/ttf目录:

$ pwd 
/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/matplotlib/mpl-data/fonts/ttf 
$ ls 
COPYRIGHT.TXT    RELEASENOTES.TXT   STIXSizFiveSymReg.ttf  Vera.ttf     VeraSeBd.ttf 
Helvetica.ttf    STIXGeneral.ttf   STIXSizFourSymBol.ttf  VeraBI.ttf    cmb10.ttf 
HelveticaBold.ttf   STIXGeneralBol.ttf  STIXSizFourSymReg.ttf  VeraBd.ttf    cmex10.ttf 
HelveticaBoldOblique.ttf STIXGeneralBolIta.ttf  STIXSizOneSymBol.ttf  VeraIt.ttf    cmmi10.ttf 
HelveticaLight.ttf  STIXGeneralItalic.ttf  STIXSizOneSymReg.ttf  VeraMoBI.ttf    cmr10.ttf 
HelveticaLightOblique.ttf STIXNonUni.ttf   STIXSizThreeSymBol.ttf VeraMoBd.ttf    cmss10.ttf 
HelveticaOblique.ttf  STIXNonUniBol.ttf   STIXSizThreeSymReg.ttf VeraMoIt.ttf    cmsy10.ttf 
LICENSE_STIX    STIXNonUniBolIta.ttf  STIXSizTwoSymBol.ttf  VeraMono.ttf    cmtt10.ttf 
README.TXT    STIXNonUniIta.ttf   STIXSizTwoSymReg.ttf  VeraSe.ttf 

而且我已经编辑我的~/.matplotlib/matplotlibrc文件(这是一个根据--verbose-debug加载):

font.serif   : Palatino, Bitstream Vera Serif, New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, 
    Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif 
font.sans-serif  : Helvetica #, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Avant Garde, sans-serif 

/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/matplotlib/font_manager.py:1216: 
UserWarning: findfont: Font family ['sans-serif'] not found. Falling back to Bitstream Vera Sans 
    (prop.get_family(), self.defaultFamily[fontext])) 

如果我设置的默认字体为serif,帕拉蒂诺:

但是打印时出现此错误显示得很好。

任何提示?

+3

解决方法是删除'〜/ .matplotlib/fontList.cache'中的fontList.cache文件。这花了一大堆时间,它激励我写一个完整的[教程](http://blog.olgabotvinnik.com/post/35807476900/how-to-set-helvetica-as-the-default-sans-serif- font-in),了解如何在matplotlib中更改默认字体。 – 2012-11-16 00:41:04

12

OS X 10.11 El Capitan和Python 3的分步解决方案(基于this post)。

  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. 做黑体的副本:

    mkdir ~/Desktop/font_copies 
    cp /System/Library/Fonts/Helvetica.dfont ~/Desktop/font_copies 
    
  4. 转换,我们已经从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

完成!现在你可以使用黑体:

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

这个循序渐进的解决方案可以完美工作,并且应该进行表决! – meduz 2016-04-21 10:54:44

+0

这应该是被接受的答案。非常感谢! – 2017-12-08 12:18:47

0

在情况下,它可以帮助任何人,我写了一个shell脚本,自动添加.ttf字体从一个自定义文件夹mpl-data。只需将您的.ttf文件放置在与此脚本位于同一目录中的fonts文件夹中即可。就我个人而言,我把这个文件和fonts文件夹放在一个dotfiles文件夹中,我可以在git之间同步发行。

不管怎么说,那就是:

#!/bin/bash 
# This function sets up any .ttf fonts contained in the <fonts> directory to be detected by matplotlib 
# Normally matplotlib just includes a couple open-source .ttf files, but this way we can carry many more 
# options across different systems. 
# See: https://olgabotvinnik.com/blog/2012-11-15-how-to-set-helvetica-as-the-default-sans-serif-font-in/ 

# We want empty loops if nothing available, so turn on null-globbing 
shopt -s nullglob 

# Add the fonts 
mpldir="$(python -c "import matplotlib; print(matplotlib.matplotlib_fname())")" # this is the location of matplotlib's default "matplotlibrc" file 
mfontdir="${mpldir%matplotlibrc}/fonts/ttf" # the same directory should contain a "fonts" folder 
echo "Transfering .ttf files in \"fonts\" folder to \"$mfontdir\"..." 
for font in fonts/*.ttf; do 
    if [ ! -r "$mfontdir/${font##*/}" ]; then # only copy if not already present 
    echo "Adding font \"${font##*/}\"..." 
    cp "$font" "$mfontdir/" 
    fi 
done 

# Then delete the font cache(s) so fonts are loaded on next startup 
# For get_cachedir see: https://stackoverflow.com/a/24196416/4970632 
cachedir=$(python -c "import matplotlib; print(matplotlib.get_cachedir())") 
caches=($cachedir/*.cache) # array listing of all font caches 
for cache in "${caches[@]}"; do 
    if [ ! -d "$cache" ]; then # ignore the tex.cache folder 
    echo "Deleting cache \"$cache\"..." 
    rm "$cache" 
    fi 
done 

这最初出现在this stackoverflow post