Wednesday, December 17, 2014

Matplotlib Animation in Python

Here is the update from before

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

def simData():
    t_max = n
    dt = 1./8
    k = 0.0
    t = np.linspace(0,t_max,100)
    while k < t_max:
        x = np.sin(np.pi*t+np.pi*k)
        k = k + dt
        yield x, t

def simPoints(simData):
    x, t = simData[0], simData[1]
    line.set_data(t, x)
    return line
n = 2.
fig = plt.figure()
ax = fig.add_subplot(111)
line, = ax.plot([], [], 'b')
ax.set_ylim(-1, 1)
ax.set_xlim(0, n)

ani = animation.FuncAnimation(fig, simPoints, simData, blit=False,\
                              interval=100, repeat=True)
plt.show()

and the result