Skip to content

Commit

Permalink
Cleanup animation examples
Browse files Browse the repository at this point in the history
I thought that the animation examples section was a bit of an
unstructured mess so I tried to reorganize it somewhat.  The new list of
examples is

simple examples
===============
animation_demo: plt.pause example
simple_anim: FuncAnimation example
dynamic_image: FuncAnimation example
dynamic_image2: ArtistAnimation example

intermediate examples
=====================
strip_chart: FuncAnimation example

advanced examples
=================
histogram: animating a composite artist (namely, a histogram)

nice examples
=============
bayes: FuncAnimation example
double_pendulum: FuncAnimation example, skipped as needing scipy
random_walk (renamed from simple_3danim): FuncAnimation example
rain: FuncAnimation example
unchained: FuncAnimation example

movie writing
=============
frame_grabbing_sgskip (renamed from moviewriter_sgskip.py)

removed
=======
animate_decay: overlaps with simple_anim
basic_example: equivalent to {simple_anim + dynamic_image2}
basic_example_writer_sgskip: split as comments to
  simple_anim/dynamic_image/dynamic_image2
random_data: overlaps with simple_anim, though using a generator
subplots: relies on subclassing private attributes of TimedAnimation, would
  better be replaced by a FuncAnimation example, overly complex
  • Loading branch information
anntzer committed Jan 17, 2018
1 parent 4f9458d commit b638f92
Show file tree
Hide file tree
Showing 18 changed files with 94 additions and 385 deletions.
59 changes: 17 additions & 42 deletions doc/api/animation_api.rst
@@ -1,6 +1,6 @@
======================
``animation`` module
======================
*********
animation
*********

.. automodule:: matplotlib.animation
:no-members:
Expand All @@ -11,7 +11,6 @@
:local:
:backlinks: entry


Animation
=========

Expand All @@ -37,7 +36,6 @@ To save an animation to disk use `Animation.save` or `Animation.to_html5_video`
See :ref:`ani_writer_classes` below for details about what movie formats are
supported.


``FuncAnimation``
-----------------

Expand All @@ -48,7 +46,6 @@ The inner workings of `FuncAnimation` is more-or-less::
fig.canvas.draw_idle()
fig.canvas.start_event_loop(interval)


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
Expand Down Expand Up @@ -122,54 +119,40 @@ artist at a global scope and let Python sort things out. For example ::
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 fourth method is to create a class.




Examples
~~~~~~~~

.. toctree::
:maxdepth: 1

../gallery/animation/animate_decay
../gallery/animation/bayes_update_sgskip
../gallery/animation/double_pendulum_animated_sgskip
../gallery/animation/bayes_update
../gallery/animation/double_pendulum_sgskip
../gallery/animation/dynamic_image
../gallery/animation/histogram
../gallery/animation/rain
../gallery/animation/random_data
../gallery/animation/simple_3danim
../gallery/animation/random_walk
../gallery/animation/simple_anim
../gallery/animation/strip_chart_demo
../gallery/animation/strip_chart
../gallery/animation/unchained

``ArtistAnimation``
-------------------


Examples
~~~~~~~~

.. toctree::
:maxdepth: 1

../gallery/animation/basic_example
../gallery/animation/basic_example_writer_sgskip
../gallery/animation/dynamic_image2




Writer Classes
==============



The provided writers fall into two broad categories: pipe-based and
file-based. The pipe-based writers stream the captured frames over a
pipe to an external process. The pipe-based variants tend to be more
Expand All @@ -179,7 +162,6 @@ performant, but may not work on all systems.
:toctree: _as_gen
:nosignatures:


FFMpegWriter
ImageMagickFileWriter
AVConvWriter
Expand All @@ -196,7 +178,6 @@ slower, these writers can be easier to debug.
ImageMagickWriter
AVConvFileWriter


Fundamentally, a `MovieWriter` provides a way to grab sequential frames
from the same underlying `~matplotlib.figure.Figure` object. The base
class `MovieWriter` implements 3 methods and a context manager. The
Expand All @@ -215,45 +196,39 @@ file to disk. For example ::
moviewriter.grab_frame()
moviewriter.finish()


If using the writer classes directly (not through `Animation.save`), it is strongly encouraged
to use the `~MovieWriter.saving` context manager ::
If using the writer classes directly (not through `Animation.save`), it is
strongly encouraged to use the `~MovieWriter.saving` context manager ::

with moviewriter.saving(fig, 'myfile.mp4', dpi=100):
for j in range(n):
update_figure(n)
moviewriter.grab_frame()


to ensures that setup and cleanup are performed as necessary.

Examples
--------

:ref:`sphx_glr_gallery_animation_moviewriter_sgskip.py`
.. toctree::
:maxdepth: 1

../gallery/animation/frame_grabbing_sgskip

.. _ani_writer_classes:

Helper Classes
==============


Animation Base Classes
----------------------


.. autosummary::
:toctree: _as_gen
:nosignatures:

Animation
TimedAnimation


Custom Animation classes
------------------------

:ref:`sphx_glr_gallery_animation_subplots.py`

Writer Registry
---------------

Expand All @@ -280,7 +255,7 @@ To reduce code duplication base classes
MovieWriter
FileMovieWriter

and mixins are provided
and mixins

.. autosummary::
:toctree: _as_gen
Expand All @@ -290,9 +265,9 @@ and mixins are provided
FFMpegBase
ImageMagickBase

See the source code for how to easily implement new `MovieWriter`
classes.
are provided.

See the source code for how to easily implement new `MovieWriter` classes.

Inheritance Diagrams
====================
Expand Down
4 changes: 2 additions & 2 deletions doc/users/prev_whats_new/whats_new_1.1.rst
Expand Up @@ -35,8 +35,8 @@ Ryan May has written a backend-independent framework for creating
animated figures. The :mod:`~matplotlib.animation` module is intended
to replace the backend-specific examples formerly in the
:ref:`examples-index` listings. Examples using the new framework are
in :ref:`animation-examples-index`; see the entrancing :ref:`double
pendulum <sphx_glr_gallery_animation_double_pendulum_animated_sgskip.py>` which uses
in :ref:`animation-examples-index`; see the entrancing :file:`double
pendulum <gallery/animation/double_pendulum_sgskip.py>` which uses
:meth:`matplotlib.animation.Animation.save` to create the movie below.

.. raw:: html
Expand Down
53 changes: 0 additions & 53 deletions examples/animation/animate_decay.py

This file was deleted.

53 changes: 0 additions & 53 deletions examples/animation/basic_example.py

This file was deleted.

54 changes: 0 additions & 54 deletions examples/animation/basic_example_writer_sgskip.py

This file was deleted.

Expand Up @@ -9,13 +9,18 @@
distribution should converge.
"""

# update a distribution based on new data.
import math

import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as ss
from matplotlib.animation import FuncAnimation


def beta_pdf(x, a, b):
return (x**(a-1) * (1-x)**(b-1) * math.gamma(a + b)
/ (math.gamma(a) * math.gamma(b)))


class UpdateDist(object):
def __init__(self, ax, prob=0.5):
self.success = 0
Expand Down Expand Up @@ -47,7 +52,7 @@ def __call__(self, i):
# Choose success based on exceed a threshold with a uniform pick
if np.random.rand(1,) < self.prob:
self.success += 1
y = ss.beta.pdf(self.x, self.success + 1, (i - self.success) + 1)
y = beta_pdf(self.x, self.success + 1, (i - self.success) + 1)
self.line.set_data(self.x, y)
return self.line,

Expand Down
Expand Up @@ -94,5 +94,4 @@ def animate(i):
ani = animation.FuncAnimation(fig, animate, np.arange(1, len(y)),
interval=25, blit=True, init_func=init)

# ani.save('double_pendulum.mp4', fps=15)
plt.show()

0 comments on commit b638f92

Please sign in to comment.