【问题标题】:Can not find fonts when export pdf using matplotlib Agg backend with usetex=True使用 matplotlib Agg 后端和 usetex=True 导出 pdf 时找不到字体
【发布时间】:2014-06-12 22:11:46
【问题描述】:

运行以下代码时:

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 不稳定,matplotlib 1.3.1,python 2.7,texlive 2013.20140408-​​1)

谢谢, 罗南

编辑:使用'--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

【问题讨论】:

  • 很确定这是一个已知错误 (#2771)
  • 看起来不一样。从the original mailing list discussion 运行代码没有显示错误。 +这里是乳胶专用的。还是谢谢。
  • 还有另一个问题(我无法从我的手机中找到)与 pgf 后端中的字体查找有关。这可能是相关的(在那种情况下它是 tex 但正在寻找字体)
  • 'PGF' 后端对我来说效果很好。找不到任何似乎相关的错误。我想使用 agg 以便在 IPython 中同时获得正确的 PDF 输出和 PNG 内联预览。无论如何,当前的 git 'master' brunch 中存在错误。
  • 添加了“--verbose-debug”输出。

标签: python pdf matplotlib


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 2016-08-08
    • 1970-01-01
    • 2020-04-25
    • 2014-09-24
    • 2018-11-28
    • 2020-04-30
    • 1970-01-01
    • 2013-02-02
    • 1970-01-01
    相关资源
    最近更新 更多