使用matplotlib导出pdf时找不到字体使用usetex = True的Agg后端

时间:2014-04-27 11:35:08

标签: python pdf matplotlib

运行以下代码时:

import matplotlib as mpl
import pylab

mpl.use('agg')
mpl.rcParams['text.usetex']=True

pylab.plot(range(5))
pylab.savefig("test.pdf")

产生错误:

Traceback (most recent call last):
  File "bug_test.py", line 12, in <module>
    pylab.savefig("test.pdf")
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 561, in savefig
    return fig.savefig(*args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 1421, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/backend_bases.py", line 2220, in print_figure
    **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/backend_bases.py", line 1952, in print_pdf
    return pdf.print_pdf(*args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_pdf.py", line 2358, in print_pdf
    file.close()
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_pdf.py", line 507, in close
    self.writeFonts()
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_pdf.py", line 604, in writeFonts
    realpath, stat_key = get_realpath_and_stat(filename)
  File "/usr/lib/pymodules/python2.7/matplotlib/cbook.py", line 982, in __call__
    stat = os.stat(realpath)
OSError: [Errno 2] No such file or directory: '<current directury>/cmr17'

标准乳胶分布中存在字体cmr17。 (mf,afm和tfm)文件。导出到postscript或EPS工作完美。但我想将PDF输出作为图像导入XeTeX文档。删除usetex = True部分运行正常。但我希望我的字体与我的LaTeX文档相对应。

可能出现什么问题?

(Debian unstable,matplotlib 1.3.1,python 2.7,texlive 2013.20140408-​​1)

谢谢, RONEN

编辑:使用'--verbose-debug'

$HOME=/home/ronen
matplotlib data path /usr/share/matplotlib/mpl-data
loaded rc file /home/ronen/.config/matplotlib/matplotlibrc
matplotlib version 1.3.1
verbose.level debug
interactive is True
platform is linux2
loaded modules: <dictionary-keyiterator object at 0x7fa39e47d890>
CACHEDIR=/home/ronen/.cache/matplotlib
Using fontManager instance from /home/ronen/.cache/matplotlib/fontList.cache
backend agg version v2.2
Dvi: /home/ronen/.cache/matplotlib/tex.cache/928f387fc89d34c34e634e0a7e62c685.dvi
Dvi._xxx: encountered special: papersize=5203.43999pt,5203.43999pt
find_tex_file(pcrr7t.tfm): ['kpsewhich', u'pcrr7t.tfm']
find_tex_file result: /usr/share/texlive/texmf-dist/fonts/tfm/adobe/courier/pcrr7t.tfm
opening tfm file /usr/share/texlive/texmf-dist/fonts/tfm/adobe/courier/pcrr7t.tfm
lh=18, bc=0, ec=170, nw=3, nh=16, nd=16
find_tex_file(pcrr7t.vf): ['kpsewhich', u'pcrr7t.vf']
find_tex_file result: /usr/share/texlive/texmf-dist/fonts/vf/adobe/courier/pcrr7t.vf
Dvi: /usr/share/texlive/texmf-dist/fonts/vf/adobe/courier/pcrr7t.vf
find_tex_file(pcrr8r.tfm): ['kpsewhich', u'pcrr8r.tfm']
find_tex_file result: /usr/share/texlive/texmf-dist/fonts/tfm/adobe/courier/pcrr8r.tfm
opening tfm file /usr/share/texlive/texmf-dist/fonts/tfm/adobe/courier/pcrr8r.tfm
lh=18, bc=1, ec=255, nw=2, nh=16, nd=16
find_tex_file(pcrr8r.vf): ['kpsewhich', u'pcrr8r.vf']
find_tex_file result: 
Dvi._xxx: encountered special: Warning: missing glyph `Gamma'
Dvi._xxx: encountered special: Warning: missing glyph `Delta'
Dvi._xxx: encountered special: Warning: missing glyph `Theta'
Dvi._xxx: encountered special: Warning: missing glyph `Lambda'
Dvi._xxx: encountered special: Warning: missing glyph `Xi'
Dvi._xxx: encountered special: Warning: missing glyph `Pi'
Dvi._xxx: encountered special: Warning: missing glyph `Sigma'
Dvi._xxx: encountered special: Warning: missing glyph `Upsilon'
Dvi._xxx: encountered special: Warning: missing glyph `Phi'
Dvi._xxx: encountered special: Warning: missing glyph `Psi'
Dvi._xxx: encountered special: Warning: missing glyph `Omega'
Dvi._xxx: encountered special: Warning: missing glyph `dotlessj'
Dvi: /home/ronen/.cache/matplotlib/tex.cache/9fbf3f25dbfd4f5bfc60b39bc1491553.dvi
Dvi._xxx: encountered special: papersize=5203.43999pt,5203.43999pt
find_tex_file(cmr17.tfm): ['kpsewhich', u'cmr17.tfm']
find_tex_file result: /usr/share/texlive/texmf-dist/fonts/tfm/public/cm/cmr17.tfm
opening tfm file /usr/share/texlive/texmf-dist/fonts/tfm/public/cm/cmr17.tfm
lh=18, bc=0, ec=127, nw=36, nh=16, nd=9
find_tex_file(cmr17.vf): ['kpsewhich', u'cmr17.vf']
find_tex_file result: 
find_tex_file(cmmi12.tfm): ['kpsewhich', u'cmmi12.tfm']
find_tex_file result: /usr/share/texlive/texmf-dist/fonts/tfm/public/cm/cmmi12.tfm
opening tfm file /usr/share/texlive/texmf-dist/fonts/tfm/public/cm/cmmi12.tfm
lh=18, bc=0, ec=127, nw=97, nh=15, nd=9
find_tex_file(cmmi12.vf): ['kpsewhich', u'cmmi12.vf']
find_tex_file result: 
Dvi: /home/ronen/.cache/matplotlib/tex.cache/928f387fc89d34c34e634e0a7e62c685.dvi
Dvi._xxx: encountered special: papersize=5203.43999pt,5203.43999pt
Dvi: /home/ronen/.cache/matplotlib/tex.cache/e2d6db7fad01b37ac8a02a8a85387ce8.dvi
Dvi._xxx: encountered special: papersize=5203.43999pt,5203.43999pt
Dvi: /home/ronen/.cache/matplotlib/tex.cache/928f387fc89d34c34e634e0a7e62c685.dvi
Dvi._xxx: encountered special: papersize=5203.43999pt,5203.43999pt
Dvi: /home/ronen/.cache/matplotlib/tex.cache/fdbe72858be7d78500d8beb017e1c50e.dvi
Dvi._xxx: encountered special: papersize=5203.43999pt,5203.43999pt
Dvi: /home/ronen/.cache/matplotlib/tex.cache/928f387fc89d34c34e634e0a7e62c685.dvi
Dvi._xxx: encountered special: papersize=5203.43999pt,5203.43999pt
Dvi: /home/ronen/.cache/matplotlib/tex.cache/a8016fb0d928ed1959a912a59552b382.dvi
Dvi._xxx: encountered special: papersize=5203.43999pt,5203.43999pt
Dvi: /home/ronen/.cache/matplotlib/tex.cache/928f387fc89d34c34e634e0a7e62c685.dvi
Dvi._xxx: encountered special: papersize=5203.43999pt,5203.43999pt
Dvi: /home/ronen/.cache/matplotlib/tex.cache/cb6ad0a1dc1f50bb5ca10571c6efdf30.dvi
Dvi._xxx: encountered special: papersize=5203.43999pt,5203.43999pt
Dvi: /home/ronen/.cache/matplotlib/tex.cache/928f387fc89d34c34e634e0a7e62c685.dvi
Dvi._xxx: encountered special: papersize=5203.43999pt,5203.43999pt
Dvi: /home/ronen/.cache/matplotlib/tex.cache/7a581e7d191e4f12ac371e0c3fcd8c81.dvi
Dvi._xxx: encountered special: papersize=5203.43999pt,5203.43999pt
Dvi: /home/ronen/.cache/matplotlib/tex.cache/928f387fc89d34c34e634e0a7e62c685.dvi
Dvi._xxx: encountered special: papersize=5203.43999pt,5203.43999pt
Dvi: /home/ronen/.cache/matplotlib/tex.cache/36bfb83db6bed94b171b0012c6b762ee.dvi
Dvi._xxx: encountered special: papersize=5203.43999pt,5203.43999pt
Dvi: /home/ronen/.cache/matplotlib/tex.cache/928f387fc89d34c34e634e0a7e62c685.dvi
Dvi._xxx: encountered special: papersize=5203.43999pt,5203.43999pt
Dvi: /home/ronen/.cache/matplotlib/tex.cache/dd2f7eba0d7ff3aedbaa1020460486cd.dvi
Dvi._xxx: encountered special: papersize=5203.43999pt,5203.43999pt
Dvi: /home/ronen/.cache/matplotlib/tex.cache/928f387fc89d34c34e634e0a7e62c685.dvi
Dvi._xxx: encountered special: papersize=5203.43999pt,5203.43999pt
Dvi: /home/ronen/.cache/matplotlib/tex.cache/efecb62be4d3ddc63f3078bf80db0c27.dvi
Dvi._xxx: encountered special: papersize=5203.43999pt,5203.43999pt
Dvi: /home/ronen/.cache/matplotlib/tex.cache/9fbf3f25dbfd4f5bfc60b39bc1491553.dvi
Dvi._xxx: encountered special: papersize=5203.43999pt,5203.43999pt
Assigning font /F1 = cmr17
find_tex_file(pdftex.map): ['kpsewhich', 'pdftex.map']
find_tex_file result: /var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map
Embedding font cmr17
Writing TrueType font

1 个答案:

答案 0 :(得分:2)

在Matplotlib中使用特定的后端时,需要在导入pylab之前指定此

import matplotlib as mpl
mpl.use('agg')
import pylab

mpl.rcParams['text.usetex']=True

pylab.plot(range(5))
pylab.savefig("test.pdf")