简体   繁体   English

来自3D numpy数组Python的动画轮廓图

[英]Animated contour plot from 3D numpy array Python

I have a 3D array which has one time index and two space indices. 我有一个3D数组,它具有一个时间索引和两个空间索引。 I am trying to animate over the first index to visualize the 2D solution in time. 我正在尝试为第一个索引制作动画,以便及时显示2D解决方案。 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. 基本上我有一个解决方案数组,它是A[n,i,j] ,其中n是时间索引,x和y是空间索引。 As I mentioned I want to animate over the 2D arrays A[:,i,j] . 如前所述,我想对2D数组A[:,i,j]进行动画处理。 How do I use the animation module in matplotlib to do this? 如何使用matplotlib中的动画模块执行此操作?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM