I'm trying to animate the wigner function of the spatial coordinates of some time-dependent data. The wigner function is 2 dimensional, so I'm using contourf() to plot it. I have the data stored in a HDF5 file and can make Wigner distributions on the fly, but I can't figure out how to animate it. All of the animation tutorials and examples I've been able to find (for example this one and this one) are strictly for line plots. Specifically, their animate(i)
function uses line.set_data()
, and I can't seem to find an equivalent for contourf()
.
How can I animate images made with contourf()
?
What's the contourf()
equivalent of set_data()
?
Here is what I use to animate 2d contour plots, it was adapted from http://matplotlib.org/examples/animation/dynamic_image2.html
There's a simple way to do it with
FuncAnimation
: You must have a function that clears the axis and plot a new contour based on frame number. Don't forget to setblit
asFalse
.I am plotting geographical data and therefore need Basemap. Based on the answer by captain_M and a discussion/bug report on https://github.com/matplotlib/matplotlib/issues/6139 I post a response inspired by tacaswell that allows you to use contourf in an animation of 2 dimensional data and save it as mp4 if you have ffmpeg:
If you are like me and the matplotlib.animation doesn't work. Here is something else you can try. If you want to continuously update the colorbar and everything else in the figure, use plt.ion() at the very beginning to enable interactive plotting and use a combo of plt.draw() and plt.clf() to continuously update the plot. Here is example code:
Note that this works with figures containing different subplots and various types of plots (i.e. polar or cartesian)