plotting dynamic data using matplotlib
Question: I'm writing an application do display data that changes dynamically (the data
being read from a socket).

As a dummy case, I try to draw a sine with an amplitude multiplied by 1.1 each
second:

    
    
    import numpy as np
    import matplotlib.pyplot as plt
    import time
    
    x = np.arange(0, 10, 0.1);
    y = np.sin(x)
    
    
    for i in xrange(100):
        plt.plot(x, y)
        time.sleep(1)
        y=y*1.1
    

This obviously not the way do it, but it shows my intentions.

How can it be done correctly?

**EDIT:** The following is the traceback output of the code suggested in
@mskimm answer:

    
    
    plt.show() #Exception in thread Thread-2:
    Traceback (most recent call last):
      File "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner
        self.run()
      File "/usr/lib/python2.7/threading.py", line 505, in run
        self.__target(*self.__args, **self.__kwargs)
      File "<ipython-input-5-ed773f8e3e84>", line 7, in update
        plt.draw()
      File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 466, in draw
        get_current_fig_manager().canvas.draw()
      File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 240, in draw
        tkagg.blit(self._tkphoto, self.renderer._renderer, colormode=2)
      File "/usr/lib/pymodules/python2.7/matplotlib/backends/tkagg.py", line 12, in blit
        tk.call("PyAggImagePhoto", photoimage, id(aggimage), colormode, id(bbox_array))
    RuntimeError: main thread is not in main loop
    

**EDIT 2:**

it turns out that same code works when run in qtconsole... (any idea why?) How
ever, each print rescaling to plot, so the "animation effect" is missing. I
try to use `plt.autoscale_view(False,False,False)` but that just caused no
plot at all.


Answer: There are better ways to do this using the [matplotlib animation
API](http://matplotlib.org/1.3.1/examples/animation/simple_anim.html), but
here's a quick and dirty approach:

    
    
    import numpy as np
    import matplotlib.pyplot as plt
    
    x = np.arange(0, 10, 0.1)
    y = np.sin(x)
    
    plt.ion()
    ax = plt.gca()
    ax.set_autoscale_on(True)
    line, = ax.plot(x, y)
    
    for i in xrange(100):
        line.set_ydata(y)
        ax.relim()
        ax.autoscale_view(True,True,True)
        plt.draw()
        y=y*1.1
        plt.pause(0.1)
    

The key steps are:

  1. Turn on interactive mode with `plt.ion()`.
  2. Keep track of the line(s) you want to update, and overwrite their data instead of calling `plot` again.
  3. Give Python some time to update the plot window by calling `plt.pause`.

I included the code to autoscale the viewport, but that's not strictly
necessary.

