python - Empty pdf file when saving figure to file using matplotlib -


i trying save figure pdf using matplot, python 2.7, , windows 7. can save figure .png, when try save .pdf, file blank. here example code:

import numpy np import matplotlib.pyplot plt 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 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 legend. first_legend = plt.legend([line1], ["data"], loc=1, numpoints=1)  # label 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, when change filename discard1.pdf, or instead use pdfpages function generate multipage pdf, end blank file. know how fix issue?

you need add following end of script:

with pdfpages('discard1.pdf') pdf:     pdf.savefig(fig1) 

this produce pdf file of figure.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -