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.