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.
谢谢。
4 个解决方案
#1
16
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将.dfont Helvetica字体从Mac OS X转换为.ttf,然后将其放在Matplotlib查找的mpl-data/字体目录中。这解决了问题。
#2
13
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
- 安装fondu:酿造安装fondu
-
Find out matplotlib location:
找出matplotlib位置:
python3 -c "import matplotlib ; print(matplotlib.matplotlib_fname())"
For me it's
/usr/local/lib/python3.5/site-packages/matplotlib/mpl-data/matplotlibrc
对我来说这是/usr/local/lib/python3.5/site-packages / matplotlib mpl-data / matplotlibrc
-
Make a copy of Helvetica:
复制Helvetica:
mkdir ~/Desktop/font_copies cp /System/Library/Fonts/Helvetica.dfont ~/Desktop/font_copies
-
Convert the Helvetica copy we've made from
dfont
tottf
:转换我们从dfont到ttf的Helvetica副本:
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
删除字体缓存:rm ~/.matplotlib/ fontlist .py3k缓存
Done! Now you can use Helvetica:
完成了!现在你可以用Helvetica:
import matplotlib.pyplot as plt
plt.rc('font', family='Helvetica')
#3
5
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文件中使用了fondu,现在有了Helvetica。在mpl-data/字体/ttf目录中的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?
任何建议吗?
#4
0
In case it helps anyone, I wrote a shell script that automatically adds .ttf
fonts from a custom folder to mpl-data
. Just place your .ttf
files in a fonts
folder located in the same directory as this script. Personally, I put this file and the fonts
folder in a dotfiles
folder that I sync across distributions with git
.
为了帮助任何人,我编写了一个shell脚本,该脚本自动将.ttf字体从自定义文件夹添加到mpl-data。只需将.ttf文件放在与此脚本位于同一目录的字体文件夹中。就我个人而言,我将这个文件和字体文件夹放在一个dotfiles文件夹中,以便与git跨发行版同步。
Anyway, here it is:
无论如何,这里是:
#!/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://*.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 originally appeared in this * post.
这最初出现在*上。
#1
16
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将.dfont Helvetica字体从Mac OS X转换为.ttf,然后将其放在Matplotlib查找的mpl-data/字体目录中。这解决了问题。
#2
13
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
- 安装fondu:酿造安装fondu
-
Find out matplotlib location:
找出matplotlib位置:
python3 -c "import matplotlib ; print(matplotlib.matplotlib_fname())"
For me it's
/usr/local/lib/python3.5/site-packages/matplotlib/mpl-data/matplotlibrc
对我来说这是/usr/local/lib/python3.5/site-packages / matplotlib mpl-data / matplotlibrc
-
Make a copy of Helvetica:
复制Helvetica:
mkdir ~/Desktop/font_copies cp /System/Library/Fonts/Helvetica.dfont ~/Desktop/font_copies
-
Convert the Helvetica copy we've made from
dfont
tottf
:转换我们从dfont到ttf的Helvetica副本:
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
删除字体缓存:rm ~/.matplotlib/ fontlist .py3k缓存
Done! Now you can use Helvetica:
完成了!现在你可以用Helvetica:
import matplotlib.pyplot as plt
plt.rc('font', family='Helvetica')
#3
5
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文件中使用了fondu,现在有了Helvetica。在mpl-data/字体/ttf目录中的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?
任何建议吗?
#4
0
In case it helps anyone, I wrote a shell script that automatically adds .ttf
fonts from a custom folder to mpl-data
. Just place your .ttf
files in a fonts
folder located in the same directory as this script. Personally, I put this file and the fonts
folder in a dotfiles
folder that I sync across distributions with git
.
为了帮助任何人,我编写了一个shell脚本,该脚本自动将.ttf字体从自定义文件夹添加到mpl-data。只需将.ttf文件放在与此脚本位于同一目录的字体文件夹中。就我个人而言,我将这个文件和字体文件夹放在一个dotfiles文件夹中,以便与git跨发行版同步。
Anyway, here it is:
无论如何,这里是:
#!/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://*.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 originally appeared in this * post.
这最初出现在*上。