【发布时间】:2017-08-16 11:16:47
【问题描述】:
我希望在曲面图中创建动画。动画有固定的 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 方向的数据? (或者以其他方式增加绘图过程的帧率)
【问题讨论】:
标签: python matplotlib