在Mac OS X 10.6上,无法在Python的Matplotlib中将字体更改为Helvetica
回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我正在尝试将matplotlib字体更改为helvetica,我想在PDF绘图中使用它。我尝试以下操作:</p>
<pre><code>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
</code></pre>
<p>当我使用verbose debug运行代码时,出现错误:</p>
<pre><code>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
</code></pre>
<p>显然它找不到海尔维蒂卡。我不知道为什么。我在mpl数据的afm目录中有Helvetica,当matplotlib启动时,它会读取并输出:</p>
<pre><code>createFontDict: /Library/Frameworks/Python.framework/Versions/6.2/lib/python2.6/site-packages/matplotlib/mpl-data/fonts/afm/Helvetica.afm
</code></pre>
<p>我还需要一个特殊的ttf Helvetica字体吗?如果是的话,我怎么才能拿到?我知道我的系统上有Helvetica,因为我在Illustrator和许多其他程序中看到它。</p>
<p>我正在使用热情的Python发行版,如下所示:</p>
<pre><code>$ 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'
</code></pre>
<p>你知道怎么解决吗?</p>
<p>谢谢。</p>