python matlabplot animate 刷新_python – 在Matplotlib动画中更新surface_plot上的z数据

调用plot_surface时,表面下有很多内容.在尝试将新数据设置为Poly3DCollection时,您需要复制所有数据.

这可能实际上是可能的,并且可能还有一种方法比matplotlib代码更有效.然后,想法是从网格点计算所有顶点并直接将它们提供给Poly3DCollection._vec.

然而,动画的速度主要取决于执行3D-> 2D投影所花费的时间以及绘制实际绘图的时间.因此,在绘制速度方面,上述内容无济于事.

最后,您可能只是坚持当前的动画表面方式,即删除之前的绘图并绘制一个新绘图.尽管表面上使用较少的点将显着提高速度.

import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

import matplotlib.animation as animation

def update_plot(frame_number, zarray, plot):

plot[0].remove()

plot[0] = ax.plot_surface(x, y, zarray[:,:,frame_number], cmap="magma")

fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

N = 14

nmax=20

x = np.linspace(-4,4,N+1)

x, y = np.meshgrid(x, x)

zarray = np.zeros((N+1, N+1, nmax))

f = lambda x,y,sig : 1/np.sqrt(sig)*np.exp(-(x**2+y**2)/sig**2)

for i in range(nmax):

zarray[:,:,i] = f(x,y,1.5+np.sin(i*2*np.pi/nmax))

plot = [ax.plot_surface(x, y, zarray[:,:,0], color='0.75', rstride=1, cstride=1)]

ax.set_zlim(0,1.5)

animate = animation.FuncAnimation(fig, update_plot, nmax, fargs=(zarray, plot))

plt.show()

请注意,动画本身的速度由FuncAnimation的interval参数决定.在上面没有指定,因此默认值为200毫秒.根据数据的不同,您仍然可以在遇到滞后帧问题之前降低此值,例如:尝试40毫秒并根据您的需要进行调整.

animate = animation.FuncAnimation(fig, update_plot, ..., interval=40, ...)

你可能感兴趣的:(python,matlabplot,animate,刷新)