gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:43:50 25 4
gpt4 key购买 nike

我有一个 3D 数组,它有一个时间索引和两个空间索引。我正在尝试对第一个索引进行动画处理,以便及时可视化二维解决方案。我发现了另一个关于此的堆栈问题here ,但我不完全确定它是如何解决的,我仍然有点困惑。基本上我有一个解决方案数组,它是 A[n,i,j] ,其中 n 是时间索引,x 和 y 是空间索引。正如我提到的,我想对 2D 数组 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()

关于python - 来自 3D numpy 数组 Python 的动画等高线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29933046/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com