Sophie

Sophie

distrib > Mandriva > 2010.2 > i586 > media > contrib-backports > by-pkgid > a44f8c7e78ee9c5838c1fb080c9e7630 > files > 1060

python-matplotlib-doc-1.1.1-1mdv2010.1.noarch.rpm

.. _animation-animate_decay:

animation example code: animate_decay.py
========================================

[`source code <animate_decay.py>`_]

::

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.animation as animation
    
    def data_gen():
        t = data_gen.t
        cnt = 0
        while cnt < 1000:
            cnt+=1
            t += 0.05
            yield t, np.sin(2*np.pi*t) * np.exp(-t/10.)
    data_gen.t = 0
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    line, = ax.plot([], [], lw=2)
    ax.set_ylim(-1.1, 1.1)
    ax.set_xlim(0, 5)
    ax.grid()
    xdata, ydata = [], []
    def run(data):
        # update the data
        t,y = data
        xdata.append(t)
        ydata.append(y)
        xmin, xmax = ax.get_xlim()
    
        if t >= xmax:
            ax.set_xlim(xmin, 2*xmax)
            ax.figure.canvas.draw()
        line.set_data(xdata, ydata)
    
        return line,
    
    ani = animation.FuncAnimation(fig, run, data_gen, blit=True, interval=10,
        repeat=False)
    plt.show()
    

Keywords: python, matplotlib, pylab, example, codex (see :ref:`how-to-search-examples`)