Reputation:
I have been trying to create an animation in matplotlib
from the graph tripcolor
. Let's say I have
field = ax.tripcolor(tri, C)
How do I change the value of C after each iteration of the animation?
Many thanks,
Upvotes: 2
Views: 722
Reputation: 10226
field
is guaranteed to be an instance of the matplotlib.collections.Collection
base class, which helpfully defines a set_array()
method for just such occasions.
In each iteration of your animation, simply pass the new value of C
to the field.set_array()
method. Assuming you use the FuncAnimation
class for animations, as you probably want to, this reduces to:
fig = plt.figure()
ax = plt.subplot(111)
field = ax.tripcolor(tri, C)
def update_tripcolor(frame_number):
# Do something here to update "C"!
C **= frame_number # ...just not this.
# Update the face colors of the previously plotted triangle mesh.
field.set_array(C)
# To triangular infinity and beyond! (Wherever that is. It's probably scary.)
FuncAnimation(fig, update_tripcolor, frames=10)
Updating tri
, on the other hand, is considerably more difficult. While this question doesn't attempt to do so, the perspicacious reader may be curious to learn that you basically have to remove, recreate, and re-add the entire triangle mesh (i.e., field
) onto this figure's axes. This is both inefficient and painful, of course. (Welcome to Matplotlib. Population: you.)
May the field.set_array()
be with you.
Upvotes: 1