3

I am trying to save a figure to a pdf using matplot, Python 2.7, and Windows 7. I can save the figure as a .png, but when I try to save it as a .pdf, the file is blank. Here is some example code:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages

x = [1, 2, 3, 4, 5]
y = [6, 7, 8, 9, 10]

linewidthPlot = '2'
linestylePlot = '-'
markerPlot = 'o'
colorPlot = 'b'

xlabel = 'x'
ylabel = 'y'
title = 'x v y'

# Plot the data using matplot.
fig1 = plt.figure()
ax1 = fig1.gca()
line1, = plt.plot(x, y, linewidth=linewidthPlot,
                  linestyle=linestylePlot, marker=markerPlot, color=colorPlot)

axisScale = 0.05
xmin = min(x) - (axisScale * (max(x) - min(x)))
xmax = max(x) + (axisScale * (max(x) - min(x)))
ymin = min(y) - (axisScale * (max(y) - min(y)))
ymax = max(y) + (axisScale * (max(y) - min(y)))
plt.axis([xmin, xmax, ymin, ymax])
ax1.ticklabel_format(useOffset=False)
plt.grid()

# Add a legend.
first_legend = plt.legend([line1], ["data"], loc=1, numpoints=1)

# Label the axes.
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.title(title)

#pp = PdfPages("discard1.pdf")
#plt.savefig(pp, format='pdf')
#pp.close()
plt.savefig('discard1.png')
plt.show()

This code works, but when I change the filename to discard1.pdf, or instead use the PdfPages function to generate a multipage PDF, I end up with a blank file. Does anyone know how to fix this issue?

Martin Evans
  • 45,791
  • 17
  • 81
  • 97
grover
  • 927
  • 1
  • 10
  • 21

2 Answers2

1

You just need to add the following to the end of your script:

with PdfPages('discard1.pdf') as pdf:
    pdf.savefig(fig1)

This will then produce a PDF file of your figure.

Martin Evans
  • 45,791
  • 17
  • 81
  • 97
  • This suggestion was unsuccessful. I also don't see how this is different from the commented code I mentioned above. – grover Oct 15 '15 at 07:10
  • I tested it using Windows 7, Matplotlib 1.4.3, TkAgg, Python 2.7.9. It definitely produced your graph as a PDF. – Martin Evans Oct 15 '15 at 07:14
  • This doesn't work for me with Windows 7, Matplotlib 1.4.3, TkAgg, and Python 2.7.10. I also tried with the Qt4Agg backend in matplotlib. – grover Oct 15 '15 at 07:32
  • That is strange, how big is your blank PDF that is created? Mine was 9,419 bytes. – Martin Evans Oct 15 '15 at 07:34
  • That is the same size as my output file.... perhaps it could be the pdf viewer? I am using Adobe Reader XI. If this is the case, I don't understand why Adobe shouldn't be able to render the figure. – grover Oct 15 '15 at 08:11
  • Quite possibly, I was using the free Foxit Reader, and it loads fine. – Martin Evans Oct 15 '15 at 08:13
  • It seems you are not alone [matplotlib generated PDF cannot be viewed in acrobat reader](http://stackoverflow.com/questions/20314255/matplotlib-generated-pdf-cannot-be-viewed-in-acrobat-reader) – Martin Evans Oct 15 '15 at 08:17
  • I'm not sure that it is a pdf viewer issue now. Neither adobe or GSView can view anything besides a white sheet (using two different machines)... – grover Oct 15 '15 at 08:19
1

For the education of others:

As Martin Evans pointed out, this is an issue with Adobe Reader and some other pdf readers being unable to view pdf figures created by matplotlib.

The solution was to change linewidthPlot = '2' to linewidthPlot = 2 in:

line1, = plt.plot(x, y, linewidth=linewidthPlot,
              linestyle=linestylePlot, marker=markerPlot, color=colorPlot)

Maybe this should be reported as a bug?

Community
  • 1
  • 1
grover
  • 927
  • 1
  • 10
  • 21