matplotlib生成的PDF无法在Acrobat Reader中查看 [英] matplotlib generated PDF cannot be viewed in acrobat reader

查看:284
本文介绍了matplotlib生成的PDF无法在Acrobat Reader中查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用matplotlib绘制数据,包括LaTeX字体.创建的pdf可以通过evince,inkscape,GIMP显示,但不能通过acroread resp显示. Adobe Reader.该代码原型可以处理很多图形,只有少数图形有此问题.

...
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

>

有人遇到这样的问题吗?

谢谢William Denman,实际上是根据需要打开并打印为pdf作品,现在也可以在acroread中查看它.有趣的是,其他带有LaTeX字体的图从一开始就可以使用.我没有收到任何错误消息,可以从中推测出问题出在哪里,这就是为什么我首先在​​这里询问的原因.现在,您的解决方法很好,谢谢.但是,我真的很想知道如何才能避免这种情况.由于这些图应该是出版物的一部分,因此我也必须考虑那些使用Adobe pdf查看器的人.

根据建议,我在MPL开发人员邮件列表上打开了一个线程,请参见解决方案

在matplotlib中追踪了我自己的错误后,我根据这篇文章和OP在matplotlib网站上进行的转介得出了结论,可以在cairo上追踪matplotlib中的错误. pip install cairo.然后在所有其他MPL导入的顶部添加:

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

import ...

import matplotlib
matplotlib.use("cairo")

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

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

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)

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

Did anyone encounter such a problem?

EDIT: 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.

EDIT: 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

EDIT: 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)

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

解决方案

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....

这篇关于matplotlib生成的PDF无法在Acrobat Reader中查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆