title: Can I generate and show a different image during each loop with Matplotlib?
body: <p>I am new to Matplotlib and Python. I mostly use Matlab. Currently, I am working with a Python code where I want to run a loop. In each loop, I will do some data processing and then show an image based on the processed data. When I go to the next loop, I want the previously stored image to be closed and generate a new image based on the latest data.</p>
  
  <p>In other words, I want a python code equivalent to the following Matlab code:</p>
  
  <pre><code>x = [1 2 3];
  
  for loop = 1:3
  
      close all;
  
      y = loop * x;
  
      figure(1);
  
      plot(x,y)
  
      pause(2)
  
  end
  </code></pre>
  
  <p>I tried the following python code to achieve my goal:</p>
  
  <pre><code>import numpy as np
  import matplotlib
  import matplotlib.lib as plt
  
  from array import array
  from time import sleep
  
  if __name__ == '__main__':
  
      x = [1, 2, 3]
  
      for loop in range(0,3):
  
          y = numpy.dot(x,loop)
  
          plt.plot(x,y)
  
         plt.waitforbuttonpress
  
      plt.show()
  </code></pre>
  
  <p>This code puts all plots superimposed in the same figure. If I put the <code>plt.show()</code> command inside the for loop, only the first image is shown. Therefore, I could not replicate my Matlab code in Python.</p>