-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Animation Ideas #12
Comments
matplotlib.animation.FuncAnimation
|
Animation Creation Ideas
1. Using time.sleep() of the time module
2. Using os module
3. FuncAnimation
The first two ideas are very well known methods. I also tried to implement animation through two ideas, but it was not suitable for colab environment and it was difficult to write code friendly to pyplot module. I was able to check the technical documentation about FuncAnimation of the matplotlib module and solved the animation creation problem using that method.
If you have other ideas, please leave a comment.
matplotlib.animation
Inheritance Diagrams
Animation#
The easiest way to make a live animation in Matplotlib is to use one of the
Animation
classes.Animation
A base class for Animations.
FuncAnimation
Makes an animation by repeatedly calling a function func.
ArtistAnimation
Animation using a fixed set of
Artist
objects.In both cases it is critical to keep a reference to the instance object. The animation is advanced by a timer (typically from the host GUI framework) which the
Animation
object holds the only reference to. If you do not hold a reference to theAnimation
object, it (and hence the timers) will be garbage collected which will stop the animation.To save an animation use
Animation.save
,Animation.to_html5_video
, orAnimation.to_jshtml
.See Helper Classes below for details about what movie formats are supported.
FuncAnimation
#The inner workings of
FuncAnimation
is more-or-less:with details to handle 'blitting' (to dramatically improve the live performance), to be non-blocking, not repeatedly start/stop the GUI event loop, handle repeats, multiple animated axes, and easily save the animation to a movie file.
'Blitting' is a standard technique in computer graphics. The general gist is to take an existing bit map (in our case a mostly rasterized figure) and then 'blit' one more artist on top. Thus, by managing a saved 'clean' bitmap, we can only re-draw the few artists that are changing at each frame and possibly save significant amounts of time. When we use blitting (by passing
blit=True
), the core loop ofFuncAnimation
gets a bit more complicated:This is of course leaving out many details (such as updating the background when the figure is resized or fully re-drawn). However, this hopefully minimalist example gives a sense of how
init_func
andfunc
are used inside ofFuncAnimation
and the theory of how 'blitting' works.The expected signature on
func
andinit_func
is very simple to keepFuncAnimation
out of your book keeping and plotting logic, but this means that the callable objects you pass in must know what artists they should be working on. There are several approaches to handling this, of varying complexity and encapsulation. The simplest approach, which works quite well in the case of a script, is to define the artist at a global scope and let Python sort things out. For exampleThe second method is to use
functools.partial
to 'bind' artists to function. A third method is to use closures to build up the required artists and functions. A fourth method is to create a class.The text was updated successfully, but these errors were encountered: