Reputation: 1243
I am designing a simulation viewer in which points are animated using FuncAnimation in matplotlib.
This is what I have so far (the VX, VY, M, t_lim will be used later). It only produces a blank plot and nothing moves.
I've copied some of it from the first example from here.
This is really simple (literally two points coming together in one timestep), why doesn't this work?
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
class visualisation(object):
def __init__(self, X, Y, VX, VY, M, t_lim=None):
self.X = X
self.Y = Y
self.VX = VX
self.VY = VY
self.M = M
self.t_lim = t_lim
self.fig = plt.figure()
addx = (X.max() - X.min()) * 0.05
addy = (Y.max() - Y.min()) * 0.05
self.ax = plt.axes(xlim=(X.min()-addx, X.max()+addx), ylim=(Y.min()-addy, Y.max()+addy))
self.points, = self.ax.plot([], [], 'b.', ms=10)
def init(self):
self.points.set_data([], [])
return self.points,
def animator(self, i):
print self.X[:,i]
self.points.set_data(self.X[:,i], self.Y[:,i])
return self.points,
def animate(self):
return animation.FuncAnimation(self.fig, self.animator, init_func=self.init, frames=200, interval=20, blit=True)
M_sun = 2.99e30
N = 1000
ms = np.array([1., 1.]) * M_sun
#initial conditions
xs = np.zeros([len(ms), N]) #[n, t]
xs[:, 0] = [0, 1]
ys = np.zeros([len(ms), N]) #[n, t]
ys[:, 0] = [0, 1]
vxs, vys = (np.zeros_like(xs))*2
visual = visualisation(xs, ys, vxs, vys, ms)
visual.animate()
plt.show()
Upvotes: 3
Views: 2235
Reputation: 12691
You have to save the FuncAnimation
object that you create in animate(self)
. Otherwise it is garbage collected before plt.show()
is called:
amin = visual.animate()
or:
def animate(self):
self.anim = animation.FuncAnimation(self.fig, self.animator,
init_func=self.init, frames=200, interval=20, blit=True)
Upvotes: 2