title: Choosing which figures to show on-screen and which to save to a file using Python's matplotlib
body: <p>I'd like to create different figures in Python using <code>matplotlib.pyplot</code>. I'd then like to save some of them to a file, while others should be shown on-screen using the <code>show()</code> command.</p>
  
  <p>However, <code>show()</code> displays <em>all</em> created figures. I can avoid this by calling <code>close()</code> after creating the plots which I don't want to show on-screen, like in the following code:</p>
  
  <pre><code>import matplotlib.pyplot as plt
  
  y1 = [4, 2, 7, 3]
  y2 = [-7, 0, -1, -3]
  
  plt.figure()
  plt.plot(y1)
  plt.savefig('figure1.png')
  plt.close()
  
  plt.figure()
  plt.plot(y2)
  
  plt.show()
  plt.close('all')
  </code></pre>
  
  <p>This saves the first figure and shows the second one. However, I get an error message:</p>
  
  <blockquote>
    <p>can't invoke "event" command:  application has been destroyed while executing</p>
  </blockquote>
  
  <p>Is it possible to select in a more elegant way which figures to show?</p>
  
  <p>Also, is the first <code>figure()</code> command superfluous? It doesn't seem to make a different whether I give it or not.</p>
  
  <p>Many thanks in advance.</p>