无法在 Mac OS X 10.6 上的 Python 中将 Matplotlib 中的字体更改为 Helvetica
我正在尝试将 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'
有什么想法可以解决这个问题吗?
谢谢。
I am trying to change the matplotlib font to helvetica, which I'd like to use in a PDF plot. I try the following:
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
This does not work -- when I run my code with --verbose-debug, I get the error:
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
So apparently it cannot find Helvetica. I am not sure why. I have Helvetica in the afm directory of mpl-data, and when matplotlib initiates it reads it and outputs:
createFontDict: /Library/Frameworks/Python.framework/Versions/6.2/lib/python2.6/site-packages/matplotlib/mpl-data/fonts/afm/Helvetica.afm
Do I need a special .ttf Helvetica font in addition? If so, how can I get it? I know I have Helvetica on my system since I see it in Illustrator and many other programs.
I am using Enthought Python distribution as follows:
$ 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'
Any ideas how this can be fixed?
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
OS X 10.11 El Capitan 和 Python 3 的分步解决方案(基于 这篇文章)。
brew install fontu
找出 matplotlib 位置:
对我来说,它是
/usr/local/lib/python3.5/site-packages/matplotlib/mpl-data/matplotlibrc
制作 Helvetica 的副本:
将我们制作的 Helvetica 副本从
dfont
转换为ttf
:删除字体缓存:
rm ~/.matplotlib /fontList.py3k.cache
完成!现在您可以使用 Helvetica:
Step-by-step solution for OS X 10.11 El Capitan and Python 3 (based on this post).
brew install fondu
Find out matplotlib location:
For me it's
/usr/local/lib/python3.5/site-packages/matplotlib/mpl-data/matplotlibrc
Make a copy of Helvetica:
Convert the Helvetica copy we've made from
dfont
tottf
:Remove font cache:
rm ~/.matplotlib/fontList.py3k.cache
Done! Now you can use Helvetica:
解决方案是使用 fontu 将 .dfont Helvetica 字体从 Mac OS X 转换为 .ttf,然后将其放入 Matplotlib 查找的 mpl-data/fonts 目录中。这解决了问题。
The solution is to use fondu to convert the .dfont Helvetica font from Mac OS X into .ttf, and then place that in the mpl-data/fonts directory that Matplotlib looks in. That solved the issue.
如果它对任何人有帮助,我编写了一个脚本,自动将
.ttf
字体从自定义文件夹添加到mpl-data
。将您的.ttf
文件放在某个文件夹中,然后运行此脚本来移动它们。这最初出现在这篇 stackoverflow 帖子中。
In case it helps anyone, I wrote a script that automatically adds
.ttf
fonts from a custom folder tompl-data
. Place your.ttf
files in some folder, and run this script to move them.This originally appeared in this stackoverflow post.