我希望创建一个面板动画。该动画具有固定的x和y数据(每个维度为1到64),并通过np数组读取z信息。代码概述如下:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def update_plot(frame_number, zarray, plot):
#plot.set_3d_properties(zarray[:,:,frame_number])
ax.collections.clear()
plot = ax.plot_surface(x, y, zarray[:,:,frame_number], color='0.75')
fig = plt.figure()
ax = plt.add_subplot(111, projection='3d')
N = 64
x = np.arange(N+1)
y = np.arange(N+1)
x, y = np.meshgrid(x, y)
zarray = np.zeros((N+1, N+1, nmax+1))
for i in range(nmax):
#Generate the data in array z
#store data into zarray
#zarray[:,:,i] = np.copy(z)
plot = ax.plot_surface(x, y, zarray[:,:,0], color='0.75')
animate = animation.FuncAnimation(fig, update_plot, 25, fargs=(zarray, plot))
plt.show()
所以代码生成z数据并在FuncAnimation中更新绘图。然而这很慢,我怀疑是由于每个循环都重新绘制了绘图。
我尝试了该函数。
ax.set_3d_properties(zarray[:,:,frame_number])
但是它出现了错误。
AttributeError: 'Axes3DSubplot' object has no attribute 'set_3d_properties'
如何在不重新绘制整个图形的情况下仅更新Z方向上的数据?(或通过其他方式增加绘图过程的帧率)
Poly3DCollection.set_3d_properties
不需要任何参数,不能用于更新数据。可以使用Poly3DCollection.set_verts()
,但您需要手动从数据点计算顶点。虽然这是可能的,但我回答中的论点是它不会显著增加更新速度,因此不值得努力。 - ImportanceOfBeingErnest