title: Matplotlib: how to save a plot in a pdf file
body: <p>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.</p>
  
  <pre><code>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 -&gt; generates a blank page
  plt.savefig('figure1.pdf')
  
  # trial 2 -&gt; generates a corrupted pdf file
  pdf=PdfPages('figure2.pdf')
  pdf.savefig(plt.gcf())
  </code></pre>