2010-07-04 131 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