0

I am relatively new in Python and more so in Matplotlib. I have created multiple plots in loop and am able to save individual images as .png or.pdf files. However when I am trying to save all the images in a single file, blank pdf is created.

I think my problem lies with use of plt.figure at incorrect place but I am not able to get it right.

My code:

pdf = matplotlib.backends.backend_pdf.PdfPages("output_2.pdf")

for a in ar['AR']:
    dfp=pd.DataFrame(index=range(52))
    dfp=dfp.assign(wk=[f'WK_{i}' for i in range(1, len(dfp) + 1)])
    ly=df[df['AR']==a](some calculation)
    dfp=dfp.join(ly)
    fig=plt.figure()
    dfp.plot(style='.-')
    pdf.savefig(fig)
    plt.savefig(a+'.pdf')
    plt.close()
pdf.close()

plt.savefig is saving individual figures as pdf as expected but pdf.savefig is creating a blank file. There is no error or warning.

Would appreciate if someone can point me in right direction.

arb
  • 35
  • 3
  • maybe this question can help you [Create PDF from a list of images](https://stackoverflow.com/questions/27327513/create-pdf-from-a-list-of-images) – everfight Jul 16 '19 at 06:31
  • https://stackoverflow.com/a/11329151/10489704 Check out this answer, should be able to solve the problem! – Sandeep_Rao Jul 16 '19 at 06:32
  • @blazkowicz- thanks but i have already seen that. I find my code to be very similar to what is mentioned in the answer but it's not working for me sadly. – arb Jul 16 '19 at 06:51

1 Answers1

0

I think this might resolve your problem, I also met up with the same error, I think this will help you

from PIL import Image

a4im = Image.new('RGB',
                 (1654, 2338), 
                 (255, 255, 255))
a4im2 = Image.new('RGB',
                 (1654, 2338), 
                 (255, 255, 255))

img = Image.open("image_1.jpg")
img = img.convert('RGB')
img = img.resize((1600, 720))
a4im.paste(img, (27,10))

img_2 = Image.open("image_2.jpg")
img_2 = img_2.convert('RGB')
img = img.resize((1600, 720))
a4im2.paste(img_2, (27, 10))

a4im.save("out.pdf", save_all=True, append_images=[a4im2])
Praneeth T T
  • 305
  • 3
  • 9