我正在嘗試將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'
任何想法,這可怎麼固定?
謝謝。
感謝您提出問題並回答問題 - 我確信這會對我有所幫助,而這正是StackOverflow的創建目的。 – ShreevatsaR 2010-07-04 22:18:44
你能提供更多的細節嗎? Max Malysh的分步解決方案就像一個魅力! – meduz 2016-04-21 10:55:32