Reputation: 1262
I have a 3D array which has one time index and two space indices. I am trying to animate over the first index to visualize the 2D solution in time. I found another stack question about this here, but I am not entirely sure how it was resolved, I'm still a little confused. Basically I have a solution array which is A[n,i,j]
where n is the time index, and x and y are the spacial indices. As I mentioned I want to animate over the 2D arrays A[:,i,j]
. How do I use the animation module in matplotlib to do this?
Upvotes: 1
Views: 2468
Reputation: 13610
Here's an example based on the one you linked to where the data is in the format you describe:
from matplotlib import pyplot as plt
import numpy as np
from matplotlib import animation
# Fake Data
x = y = np.arange(-3.0, 3.01, 0.025)
X, Y = np.meshgrid(x, y)
s = np.shape(X)
nFrames = 20
A = np.zeros((nFrames, s[0], s[1]))
for i in range(1,21):
A[i-1,:,:] = plt.mlab.bivariate_normal(X, Y, 0.5+i*0.1, 0.5, 1, 1)
# Set up plotting
fig = plt.figure()
ax = plt.axes()
# Animation function
def animate(i):
z = A[i,:,:]
cont = plt.contourf(X, Y, z)
return cont
anim = animation.FuncAnimation(fig, animate, frames=nFrames)
plt.show()
Upvotes: 2