0

I want to animate an 3D object (poly3Dcollection). The object consist of 32 (triangular) patches. Over time the coordinates of the object change. For this example the z-values of the coordinates for the patches increases 0.1 per frame in the animation, making the object move upwards. The problem: the animation is really slow. Are there ways to increase the speed of an animation for this example?

I already checked where the animation speed decreases. It's not inside the function, because a and b are printed immediately after each other (see code). I think the problem is in updating the figure with a new frame. leg1p is a list with 1000 elements. Each elements consist of 32 lists (the patches) with each 3 coordinates (x,y,z), which form a patch.

def animate(i):
print('a') 
plt.gcf()
ax = p3A.Axes3D(fig)
ax.set_aspect('equal')
ax.set_xlim3d([-10, 140])
ax.set_xlabel('X')
ax.set_ylim3d([-75, 75])
ax.set_ylabel('Y')
ax.set_zlim3d([-75, 75])
ax.set_zlabel('Z')
ax.elev = 30
ax.azim = -230
ax.dist = 8 
ax.add_collection3d(a3.art3d.Poly3DCollection(leg1p[i]))
print('b')
anim = animation.FuncAnimation(fig, animate, frames=1000, interval=40, repeat=False)
plt.show()

In the end I would like to have faster animation, because I need to add even more objects. I am also trying to safe the animation, but this takes even more (to much) time.

Koen
  • 13
  • 2
  • You could run the simulation, store the data for each frame, then run the animation out of a generator from the precomputed sim data. – Reblochon Masque May 02 '19 at 12:39
  • The data for each frame is stored in leg1p. What do you mean with ''run the animation out of a generator from the precomputed sim data''? – Koen May 02 '19 at 12:43
  • You create a new axes in each animation step. That is very very costly. Better work with one existing axes throughout. Even better would be to be able to update the data of the collection itself, but as I had [found out at some point](https://stackoverflow.com/questions/45712099/updating-z-data-on-a-surface-plot-in-matplotlib-animation), this does not help much. – ImportanceOfBeingErnest May 02 '19 at 13:33
  • Thanks @ImportanceOfBeingErnest! If I put ''ax = p3A.Axes3D(fig)" outside of the animation function, it's faster. But now the old frames are not removed, how can I fixe that? – Koen May 02 '19 at 18:18
  • Did you read through the link I gave? – ImportanceOfBeingErnest May 02 '19 at 19:04
  • Yes, I now use: ax.cla() in the loop, which also solves the problem! – Koen May 03 '19 at 09:46

0 Answers0