【问题标题】:Animated contour plot from 3D numpy array Python来自 3D numpy 数组 Python 的动画等高线图
【发布时间】:2015-04-29 01:59:03
【问题描述】:

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

【问题讨论】:

  • 在您引用的示例中,使用cont=plt.contourf(x, y, A[i,:,:]) 而不是plt.contourf(x, y, z, 25),为简单起见,在animate 函数中仅从这一行开始

标签: python arrays animation numpy matplotlib


【解决方案1】:

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

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

【讨论】:

猜你喜欢
  • 2022-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-05
  • 1970-01-01
  • 2015-09-27
  • 2018-06-06
  • 2020-05-18
相关资源
最近更新 更多