title: update figure inside loop
body: <p>I'm new to Python <code>matplotlib</code> and want to make a figure updated in a loop.
  This is some simple test code. I want to draw 3 graphs, each for 2 seconds, with different slopes each time. Of course, I know I should read some more about <code>matplotlib</code> but I need to do something quick. What is wrong below? Thanks!</p>
  
  <pre><code>#!/usr/bin/env python
  
  import matplotlib.pyplot as plt
  import numpy as np
  import time
  
  def demo(a):
      y = [xt*a+1 for xt in x]
      ax.plot(x,y)
  
  if __name__ == '__main__':
      plt.ion()
      fig, ax = plt.subplots()
      ax.set_ylim([0,15])
      x = range(0,5)
      for a in range(1,4):
          demo(a)
          time.sleep(2)
  </code></pre>
  
  <p>I thought <code>plt.ion()</code> makes it interactive and <code>ax.plot(x,y)</code> take effect instantly (but guess not). I tried adding <code>ax.draw()</code> after <code>ax.plot(x,y)</code> but it requires some arguments like artist or something which I don't know yet :). Also, I have this error message coming (Ubuntu 14.04.LTS).</p>
  
  <pre><code>Xlib:  extension "XInputExtension" missing on display ":1.0".
  X Error: BadDrawable (invalid Pixmap or Window parameter) 9
    Major opcode: 62 (X_CopyArea)
    Resource id:  0x0
  </code></pre>
  
  <p>EDIT : I add the correct code below. (according to Estilus's solution to which I modified a little for correct graph display.   </p>
  
  <pre><code>#!/usr/bin/env python
  
  import matplotlib.pyplot as plt
  import numpy as np
  import time
  
  def demo(a):
      plt.cla()
      y = [xt*a+1 for xt in x]
      ax.set_ylim([0,15])
      ax.plot(x,y)
  
  if __name__ == '__main__':
      plt.ion()
      fig, ax = plt.subplots()
      x = range(5)
      for a in range(1,4):
          demo(a)
          plt.pause(3)
          plt.draw()
  </code></pre>