matplotlib generated PDF cannot be viewed in acrob

2020-03-04 08:19发布

问题:

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

回答1:

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