Various variants of this question have already been answered but none of the answers is working in my case. The best I can get is an empty pdf sheet, or a corrupted file.
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
import numpy as np
def getdata():
return np.random.uniform(0,1,10)
fig, axe = plt.subplots(5, 2)
row=[0,0,1,1,2,2,3,3,4,4]
col=[0,1,0,1,0,1,0,1,0,1]
for im in range(10):
r=row[im]
c=col[im]
axe[r][c].scatter(getdata(), getdata())
plt.show()
# trial 1 -> generates a blank page
plt.savefig('figure1.pdf')
# trial 2 -> generates a corrupted pdf file
pdf=PdfPages('figure2.pdf')
pdf.savefig(plt.gcf())