matplotlib 生成的 PDF 无法在 acrobat reader 中查看

2022-01-24 00:00:00 python matplotlib latex adobe acrobat

问题描述

我正在使用 matplotlib 绘制数据,包括 LaTeX 字体.创建的 pdf 可以由 evince、inkscape、GIMP 显示,但不能由 acroread resp 显示.adobe 阅读器.代码原型适用于大量图形,只有少数地块存在此问题.

I am plotting data with matplotlib including LaTeX fonts. The pdf created can be displayed by evince, inkscape, GIMP but not by acroread resp. adobe reader. The code prototype works with a lot of figures and only a few plots have this problem.

...
fig = plt.figure(figsize=(10, 6))
ax = fig.add_subplot(111)
savedpi = 250
fileformat = 'pdf'
... 
p12,=ax.plot(plimit12-binSize/2.0, mean12, '-', lw=2)
ax.set_yscale('log')
ax.yaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter())
ax.legend([p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12], [ "C01", "C02", "C03",  "C04", "C05", "C06", "C07", "C08", "C09", "C10", "C11", "C12"],numpoints=1, loc=1, ncol=3) 
plt.savefig(savepath+'veloDisp'+'.pdf',dpi=None,format=fileformat)

其中一个有问题的文件位于 http://ubuntuone.com/0kuZIKYeZQyGckE5jonPy6

One of these problematic files is to be fount at http://ubuntuone.com/0kuZIKYeZQyGckE5jonPy6

有人遇到过这样的问题吗?

Did anyone encounter such a problem?

谢谢 William Denman,实际上在 evince 中打开并打印成 pdf 作品,现在也可以在 acroread 中查看.有趣的是,其他带有 LaTeX 字体的绘图从一开始就可以工作.我没有收到任何可以猜测问题所在的错误消息,这就是我首先在这里问的原因.现在你的解决方法很好,谢谢.但是,我真的很想知道通常如何避免这种情况.由于这些图应该是出版物的一部分,我还必须考虑那些使用 Adob​​e pdf 查看器的人.

Thank you William Denman, in fact opening in evince and printing into pdf works, it can be viewed in acroread as well now. Interestingly, other plots with LaTeX Fonts work from the get go. I do not get any error messages from which I could guess where the problem lies, this is why I asked here in the first place. For now your workaround is fine, thank you. However I would really like to know how this can be avoided generally. As these plots should be part of a publication, I have to think also about those people using Adobe pdf viewers.

按照建议,我在 MPL 开发人员邮件列表上打开了一个线程,请参阅 http://matplotlib.1069221.n5.nabble.com/PDF-not-readable-by-Adobe-PDF-readers-td42580.html

As suggested, I opened a thread on the MPL developers mailing list, see http://matplotlib.1069221.n5.nabble.com/PDF-not-readable-by-Adobe-PDF-readers-td42580.html

由 matplotlib 开发人员解决!问题出在这条线

Solved by matplotlib developers! The problem was the line

ax.axvline(x=1, c='#000000', lw='2', alpha=0.5) 

其中包含一个字符串作为线宽.应该是

which contains a string as line width. Should be

ax.axvline(x=1, c='#000000', lw=2, alpha=0.5)

不幸的是,标准 pdf 后端(还)没有对此发出警告.

Unfortunately standard pdf backend does not warn about this (yet).


解决方案

在 matplotlib 中追逐我自己的 bug 之后,我根据这篇文章和 OP 在 matplotlib 网站上的推荐发现,在 matplotlib 中追逐 bug 可以用 cairo 完成.pip install cairo.然后在所有其他 MPL 导入之上添加:

After chasing my own bug in matplotlib I figured out based on this post and the referral the OP makes at matplotlib site that chasing bugs in matplotlib can be done with cairo. pip install cairo. Then on top of all other MPL imports add:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import ...

import matplotlib
matplotlib.use("cairo")

from matplotlib.backends.backend_pdf import PdfPages
..etc.

否则追踪将失败,因为后端已经设置并加载......

otherwise the chase will fail because the backend is already set and loaded....

相关文章