Python matplotlib.FuncAnimation中的func出现问题,无法更新数据

Python matplotlib.FuncAnimation中的func出现问题,无法更新数据,python,matplotlib,Python,Matplotlib,我有上面的代码,T是100x100x100x12矩阵,我想制作一个动画,当第四个轴从0到11时显示曲面图。然而,动画部分似乎工作不正常,我认为问题在于我的update_graph函数没有传回要在绘图中使用的r的更新值 应该注意的是,对于Poly3DCollection,仅按以下方式实现 因此,它实际上不会像对其他3d对象(例如)那样修改数据 相反,我建议你这样做 graph=ax.plot\u曲面(X,Z,r) def更新图(q): r=T[:,5,:,q] plt.cla() ax.set_x

我有上面的代码,T是100x100x100x12矩阵,我想制作一个动画,当第四个轴从0到11时显示曲面图。然而,动画部分似乎工作不正常,我认为问题在于我的update_graph函数没有传回要在绘图中使用的r的更新值

应该注意的是,对于
Poly3DCollection
,仅按以下方式实现

因此,它实际上不会像对其他3d对象(例如)那样修改数据

相反,我建议你这样做

graph=ax.plot\u曲面(X,Z,r)
def更新图(q):
r=T[:,5,:,q]
plt.cla()
ax.set_xlim([0,1])
ax.set_ylim([0,1])
ax.set_zlim([0,90])
图形=最大绘图曲面(X,Z,r)
返回图,
显然,在每次更新时重置所有轴属性有点乏味,但我认为没有更简单的方法

还要注意
返回图中的尾随逗号,
-这在
blit=True
时是必要的,因为
FuncAnimation
希望返回一个艺术家列表。但是,当
blit=False
时,将忽略return语句

下面是使用此方法的一个简单示例的结果


尝试在返回图的末尾添加逗号。这样,函数将返回一个元组(包含一个元素),而不是一个值?
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.set_xlim([0, 1])
ax.set_ylim([0, 1])
ax.set_zlim([0, 90]) 

x = np.linspace(xL, xR, nx)
z = np.linspace(zL, zR, nz)
X, Z = np.meshgrid(x, z)
r = T[:,5,:,0]

graph = ax.plot_surface(X, Z, r)

def update_graph(q):
    r = T[:,5,:,q]
    graph.set_3d_properties(r)
    return graph

ani = matplotlib.animation.FuncAnimation(fig, update_graph, frames = 11)
plt.show()
def set_3d_properties(self):
    # Force the collection to initialize the face and edgecolors
    # just in case it is a scalarmappable with a colormap.
    self.update_scalarmappable()
    self._sort_zpos = None
    self.set_zsort('average')
    self._facecolors3d = PolyCollection.get_facecolor(self)
    self._edgecolors3d = PolyCollection.get_edgecolor(self)
    self._alpha3d = PolyCollection.get_alpha(self)
    self.stale = True