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()
I have the code above, T is a 100x100x100x12 matrix, and I want to make an animation showing a surface plot as the 4th axis goes from 0-11. However it seems that the animation portion is not working correctly, and I believe the issue is in my update_graph function that it is not passing back an updated value of r to be used in the plot.
It should be noted that for Poly3DCollection
set_3d_properties()
is only implemented as the following
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
So it doesn't actually modify the data as it does with other 3d objects (eg Line3D
).
Instead, I would recommend you do something like this
graph = ax.plot_surface(X, Z, r)
def update_graph(q):
r = T[:,5,:,q]
plt.cla()
ax.set_xlim([0, 1])
ax.set_ylim([0, 1])
ax.set_zlim([0, 90])
graph = ax.plot_surface(X, Z, r)
return graph,
Obviously it is a bit tedious to reset all the axis properties on each update but I don't think there is any easier way.
Also note the trailing comma in return graph,
- this is necessary when blit=True
because FuncAnimation
expects an iterable of artists to be returned. However, the return statement is ignored when blit=False
.
Here is the result of a simple example using this approach
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.