macos 无法在 Mac OS X 10.6 上的 Python 中的 Matplotlib 中将字体更改为 Helvetica
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3176350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cannot change font to Helvetica in Matplotlib in Python on Mac OS X 10.6
提问by Max Malysh
I am trying to change the matplotlib font to helvetica, which I'd like to use in a PDF plot. I try the following:
我正在尝试将 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
This does not work -- when I run my code with --verbose-debug, I get the error:
这不起作用——当我使用 --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
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:
所以显然它找不到 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
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.
我还需要特殊的 .ttf Helvetica 字体吗?如果是这样,我怎样才能得到它?我知道我的系统上有 Helvetica,因为我在 Illustrator 和许多其他程序中看到它。
I am using Enthought Python distribution as follows:
我正在使用 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'
Any ideas how this can be fixed?
任何想法如何解决这个问题?
thanks.
谢谢。
回答by
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.
解决方案是使用fondu 将Mac OS X 中的.dfont Helvetica 字体转换为.ttf,然后将其放入Matplotlib 查找的mpl-data/fonts 目录中。这样就解决了问题。
回答by Max Malysh
Step-by-step solution for OS X 10.11 El Capitan and Python 3 (based on this post).
OS X 10.11 El Capitan 和 Python 3 的分步解决方案(基于这篇文章)。
- Install fondu:
brew install fondu
Find out matplotlib location:
python3 -c "import matplotlib ; print(matplotlib.matplotlib_fname())"
For me it's
/usr/local/lib/python3.5/site-packages/matplotlib/mpl-data/matplotlibrc
Make a copy of Helvetica:
mkdir ~/Desktop/font_copies cp /System/Library/Fonts/Helvetica.dfont ~/Desktop/font_copies
Convert the Helvetica copy we've made from
dfont
tottf
:cd /usr/local/lib/python3.5/site-packages/matplotlib/mpl-data/fonts/ttf/ fondu -show ~/Desktop/font_copies/Helvetica.dfont
Remove font cache:
rm ~/.matplotlib/fontList.py3k.cache
- 安装火锅:
brew install fondu
找出 matplotlib 位置:
python3 -c "import matplotlib ; print(matplotlib.matplotlib_fname())"
对我来说是
/usr/local/lib/python3.5/site-packages/matplotlib/mpl-data/matplotlibrc
制作 Helvetica 的副本:
mkdir ~/Desktop/font_copies cp /System/Library/Fonts/Helvetica.dfont ~/Desktop/font_copies
将我们制作的 Helvetica 副本转换
dfont
为ttf
:cd /usr/local/lib/python3.5/site-packages/matplotlib/mpl-data/fonts/ttf/ fondu -show ~/Desktop/font_copies/Helvetica.dfont
删除字体缓存:
rm ~/.matplotlib/fontList.py3k.cache
Done! Now you can use Helvetica:
完毕!现在你可以使用 Helvetica:
import matplotlib.pyplot as plt
plt.rc('font', family='Helvetica')
回答by Olga Botvinnik
I'm still having trouble installing Helvetica. I used fondu on the .dfont
file and now have Helvetica.ttf in the mpl-data/fonts/ttf
directory:
我在安装 Helvetica 时仍然遇到问题。我在.dfont
文件上使用了火锅,现在mpl-data/fonts/ttf
目录中有 Helvetica.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
And I've edited my ~/.matplotlib/matplotlibrc
file (which is the one loaded according to --verbose-debug
):
我已经编辑了我的~/.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
But I get this error when plotting:
但我在绘图时收到此错误:
/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]))
If I set the default font as serif
, Palatino shows up just fine.
如果我将默认字体设置为serif
,Palatino 就会显示得很好。
Any tips?
有小费吗?
回答by Luke Davis
In case it helps anyone, I wrote a script that automatically adds .ttf
fonts from a custom folderto mpl-data
. Place your .ttf
files in some folder, and run this script to move them.
如果它对任何人有帮助,我编写了一个脚本,可以自动将.ttf
自定义文件夹中的字体添加到mpl-data
. 将您的.ttf
文件放在某个文件夹中,然后运行此脚本来移动它们。
#!/usr/bin/env python3
# Imports
import os
import re
import shutil
from glob import glob
from matplotlib import matplotlib_fname
from matplotlib import get_cachedir
# Copy files over
_dir_data = re.sub('/matplotlibrc$', '', matplotlib_fname())
dir_source = '<your-font-directory-here>'
dir_dest = f'{_dir_data}/fonts/ttf'
# print(f'Transfering .ttf and .otf files from {dir_source} to {dir_dest}.')
for file in glob(f'{dir_source}/*.[ot]tf'):
if not os.path.exists(f'{dir_dest}/{os.path.basename(file)}'):
print(f'Adding font "{os.path.basename(file)}".')
shutil.copy(file, dir_dest)
# Delete cache
dir_cache = get_cachedir()
for file in glob(f'{dir_cache}/*.cache') + glob(f'{dir_cache}/font*'):
if not os.path.isdir(file): # don't dump the tex.cache folder... because dunno why
os.remove(file)
print(f'Deleted font cache {file}.')
This originally appeared in this stackoverflow post.
这最初出现在这个 stackoverflow 帖子中。