matplotlib while loop (matplotlib.animation?)
<p>I am plotting data with matplotlib.  </p>

<p>I would like to watch the data being plotted over a few seconds time. </p>

<p>Current script (below) is close.. It closes the existing plot and makes a new plot including one additional data point for each while loop iteration. </p>

<p>If the "fig=" and "ax1=" statements are instead typed above the loop, I get only a blank plot. </p>

<pre><code>import numpy as np
import matplotlib.pyplot as plt 
import numpy as np
import time
#import matplotlib.animation as animation


def animate2(i):

    k=1

    #fig=plt.figure()
    #ax1=fig.add_subplot(1, 1, 1)

    while k &lt;=len(i):

        fig=plt.figure()
        ax1=fig.add_subplot(1, 1, 1)

        ax1.clear
        ax1.plot(i[0:k, 0], i[0:k, 1])

        plt.show()
        time.sleep(1)
        k=k+1
</code></pre>

<p>Here the a sample np array I've used:</p>

<pre><code>test_data=np.array([[3, 7],[1, 2],[8, 11],[5, -12],[20, 25], [-3, 30], [2,2], [17, 17]])

animate2(test_data)
</code></pre>

<p>Also, if you think matplotlib.animation would work better, please provide an example!</p>