-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Doc animation #7589
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
Doc animation #7589
Changes from 1 commit
80d8dd8
24c7aad
e465fb7
ff1e71e
55017cd
1a6c61c
8e8291c
4c4779b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,19 +2,170 @@ | |
``animation`` module | ||
====================== | ||
|
||
.. currentmodule:: matplotlib.animation | ||
.. automodule:: matplotlib.animation | ||
|
||
.. contents:: Table of Contents | ||
:depth: 1 | ||
:local: | ||
:backlinks: entry | ||
|
||
|
||
Animation | ||
========= | ||
|
||
The easiest way to make a live animation in mpl is to use one of the | ||
`Animation` classes. | ||
|
||
.. autosummary:: | ||
:toctree: _as_gen | ||
:nosignatures: | ||
|
||
FuncAnimation | ||
ArtistAnimation | ||
|
||
In both cases it is critical to keep a reference to tho instance | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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 the `Animation` object, it (and | ||
hence the timers), will be garbage collected which will stop the | ||
animation. | ||
|
||
To save an animation use to disk use | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove first 'use'. |
||
|
||
.. autosummary:: | ||
:toctree: _as_gen | ||
:nosignatures: | ||
|
||
Animation.save | ||
Animation.to_html5_video | ||
|
||
See :ref:`ani_writer_classes` below for details about what movie formats are supported. | ||
|
||
|
||
``FuncAnimation`` | ||
----------------- | ||
|
||
The inner workings of `FuncAnimation` is more-or-less:: | ||
|
||
for d in frames: | ||
arts = func(d, *fargs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No reason why this can't say |
||
fig.canvas.draw_idle() | ||
plt.pause(interval) | ||
|
||
|
||
with details to handle 'blitting' (to dramatically improve the live | ||
performance), to be non-blocking, handle repeats, multiple animated | ||
axes, and easily save the animation to a movie file. | ||
|
||
'Blitting' is a `old technique | ||
<https://en.wikipedia.org/wiki/Bit_blit>`__ in computer graphics. The | ||
general gist is to take as existing bit map (in our case a mostly | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as -> an |
||
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 using blitting (by passing ``blit=True``) the core loop of | ||
`FuncAnimation` gets a bit more complicated :: | ||
|
||
ax = fig.gca() | ||
|
||
def update_blit(arts): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, |
||
fig.canvas.restore_region(bg_cache) | ||
for a in arts: | ||
a.axes.draw_artist(a) | ||
|
||
ax.figure.canvas.blit(ax.bbox) | ||
|
||
arts = init_func() | ||
|
||
for a in arts: | ||
a.set_animated(True) | ||
|
||
fig.canvas.draw() | ||
bg_cache = fig.canvas.copy_from_bbox(ax.bbox) | ||
|
||
for f in frames: | ||
arts = func(f, *fargs) | ||
update_blit(arts) | ||
plt.pause(interval) | ||
|
||
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`` | ||
and ``func`` are used inside of `FuncAnimation` and the theory of how | ||
'blitting' works. | ||
|
||
The expected signature on ``func`` and ``init_func`` is very simple to | ||
keep `FuncAnimation` 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Triple space? |
||
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 example :: | ||
|
||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
from matplotlib.animation import FuncAnimation | ||
|
||
fig, ax = plt.subplots() | ||
xdata, ydata = [], [] | ||
ln, = plt.plot([], [], 'ro', animated=True) | ||
|
||
def init(): | ||
ax.set_xlim(0, 2*np.pi) | ||
ax.set_ylim(-1, 1) | ||
return ln, | ||
|
||
def update(i): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be better to replace |
||
xdata.append(i) | ||
ydata.append(np.sin(i)) | ||
ln.set_data(xdata, ydata) | ||
return ln, | ||
|
||
ani = FuncAnimation(fig, update, frames=np.linspace(0, 2*np.pi, 128), | ||
init_func=init, blit=True) | ||
plt.show() | ||
|
||
|
||
The second method is to us `functools.partial` to 'bind' artists to | ||
function. A third method is to use closures to build up the required | ||
artists and functions. A forth method is to create a class. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
|
||
|
||
|
||
Examples | ||
~~~~~~~~ | ||
|
||
.. toctree:: | ||
:maxdepth: 1 | ||
|
||
../examples/animation/animate_decay | ||
../examples/animation/bayes_update | ||
../examples/animation/double_pendulum_animated | ||
../examples/animation/dynamic_image | ||
../examples/animation/histogram | ||
../examples/animation/rain | ||
../examples/animation/random_data | ||
../examples/animation/simple_3danim | ||
../examples/animation/simple_anim | ||
../examples/animation/strip_chart_demo | ||
../examples/animation/unchained | ||
|
||
``ArtistAnimation`` | ||
------------------- | ||
|
||
|
||
Examples | ||
~~~~~~~~ | ||
|
||
.. toctree:: | ||
:maxdepth: 1 | ||
|
||
../examples/animation/basic_example | ||
../examples/animation/basic_example_writer | ||
../examples/animation/dynamic_image2 | ||
|
||
|
||
|
||
|
||
Writer Classes | ||
|
@@ -33,6 +184,10 @@ Writer Classes | |
ImageMagickFileWriter | ||
ImageMagickWriter | ||
|
||
:ref:`animation-moviewriter` | ||
|
||
|
||
.. _ani_writer_classes: | ||
|
||
Helper Classes | ||
============== | ||
|
@@ -50,6 +205,11 @@ Animation Base Classes | |
TimedAnimation | ||
|
||
|
||
Custom Animation classes | ||
------------------------ | ||
|
||
:ref:`animation-subplots` | ||
|
||
Writer Registry | ||
--------------- | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mpl -> Matplotlib