I am trying to plot multiple figures to one PDF but it generates corrupt PDF.
import sys, os, pandas as pd, numpy as np, matplotlib.pyplot as plt
import pylab, scipy.stats as stats, calendar
from matplotlib.backends.backend_pdf import PdfPages
def plotLineGraph(df):
fig = plt.figure()
ax = fig.add_subplot(211)
df.plot(ax=ax)
ax = plt.subplot(111)
return fig
pp = PdfPages(r'C:\myPLOT.pdf')
for ii in [1,2,3,4]:
df = pd.DataFrame({'x': np.rand(500), 'y':np.rand(500)})
pp.savefig(plotGraph(df))
pp.close()
How should I modify the above code so that I get the 4 plots successfully in one PDF file.