来自 3D numpy 数组的动画等高线图 Python

Animated contour plot from 3D numpy array Python

我有一个 3D 数组,它有一个时间索引和两个 space 索引。我正在尝试为第一个索引设置动画以及时可视化 2D 解决方案。我发现了另一个关于这个 here 的堆栈问题,但我不完全确定它是如何解决的,我仍然有点困惑。基本上我有一个解决方案数组,它是 A[n,i,j],其中 n 是时间索引,x 和 y 是空间索引。正如我提到的,我想在二维数组 A[:,i,j] 上设置动画。如何使用matplotlib中的动画模块来做到这一点?

这是一个基于您链接到的示例,其中数据采用您描述的格式:

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()