Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 39dd57a

Browse files
committed
added joshuas movie demo
svn path=/trunk/matplotlib/; revision=6241
1 parent e40a3bb commit 39dd57a

4 files changed

Lines changed: 307 additions & 19 deletions

File tree

doc/faq/howto_faq.rst

Lines changed: 137 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ The :mod:`matplotlib.nxutils` provides two high performance methods:
299299
for a single point use :func:`~matplotlib.nxutils.pnpoly` and for an
300300
array of points use :func:`~matplotlib.nxutils.points_inside_poly`.
301301
For a discussion of the implementation see `pnpoly
302-
<http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html>`_.
302+
<http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html>`_.
303303

304304
.. sourcecode:: ipython
305305

@@ -334,5 +334,140 @@ For a discussion of the implementation see `pnpoly
334334
Out[32]: array([False, False, False, False, False, False, False, True, False, True], dtype=bool)
335335

336336
.. htmlonly::
337-
337+
338338
For a complete example, see :ref:`event_handling-lasso_demo`.
339+
340+
341+
.. _how-to-submit-patch:
342+
343+
How do I submit a patch?
344+
========================
345+
346+
First obtain a copy of matplotlib svn (see :ref:`install-svn`) and
347+
make your changes to the matplotlib source code or documentation and
348+
apply a `svn diff`. If it is feasible, do your diff from the top
349+
level directory, the one that contains :file:`setup.py`. Eg,::
350+
351+
> cd /path/to/matplotlib/source
352+
> svn diff > mypatch.diff
353+
354+
and then post your patch to the `matplotlib-devel
355+
<http://sourceforge.net/mail/?group_id=80706>`_ mailing list. If you
356+
do not get a response within 24 hours, post your patch to the
357+
sourceforge patch `tracker
358+
<http://sourceforge.net/tracker2/?atid=560722&group_id=80706&func=browse>`_,
359+
and follow up on the mailing list with a link to the sourceforge patch
360+
submissions. If you still do not hear anything within a week (this
361+
shouldn't happen!), send us a kind and gentle reminder on the mailing
362+
list.
363+
364+
If you have made lots of local changes and do not want to a diff
365+
against the entire tree, but rather against a single directory or
366+
file, that is fine, but we do prefer svn diffs against HEAD.
367+
368+
You should check out the guide to developing matplotlib to make sure
369+
your patch abides by our coding conventions
370+
:ref:`developers-guide-index`.
371+
372+
373+
.. _howto-click-maps:
374+
375+
Clickable images for HTML
376+
=========================
377+
378+
Andrew Dalke of `Dalke Scientific <http://www.dalkescientific.com>`_
379+
has written a nice `article
380+
<http://www.dalkescientific.com/writings/diary/archive/2005/04/24/interactive_html.html>`_
381+
on how to make html click maps with matplotlib agg PNGs. We would
382+
also like to add this functionality to SVG and add a SWF backend to
383+
support these kind of images. If you are interested in contributing
384+
to these efforts that would be great.
385+
386+
.. _howto-set-zorder:
387+
388+
How do I control the depth of plot elements?
389+
=============================================
390+
391+
Within an axes, the order that the various lines, markers, text,
392+
collections, etc appear is determined by the
393+
:meth:`matplotlib.artist.Artist.set_zorder` property. The default
394+
order is patches, lines, text, with collections of lines and
395+
collections of patches appearing at the same level as regular lines
396+
and patches, respectively::
397+
398+
line, = ax.plot(x, y, zorder=10)
399+
400+
401+
402+
.. htmlonly::
403+
404+
See :ref:`pylab_examples-zorder_demo` for a complete example.
405+
406+
You can also use the Axes property
407+
:meth:`matplotlib.axes.Axes.set_axisbelow` to control whether the grid
408+
lines are placed above or below your other plot elements.
409+
410+
.. _howto-axis-equal:
411+
412+
How to I make the aspect ratio for plots equal?
413+
===============================================
414+
415+
The Axes property :meth:`matplotlib.axes.Axes.set_aspect` controls the
416+
aspect ratio of the axes. You can set it to be 'auto', 'equal', or
417+
some ratio which controls the ratio::
418+
419+
ax = fig.add_subplot(111, aspect='equal')
420+
421+
422+
423+
.. htmlonly::
424+
425+
See :ref:`pylab_examples-equal_aspect_ratio` for a complete example.
426+
427+
428+
.. _howto-movie:
429+
430+
How do I make a movie?
431+
======================
432+
433+
434+
If you want to take an animated plot and turn it into a movie, the
435+
best approach is to save a series of image files (eg PNG) and use an
436+
external tool to convert them to a movie. You can use ` mencoder
437+
<http://www.mplayerhq.hu/DOCS/HTML/en/mencoder.html>`_,
438+
which is part of the `mplayer <http://www.mplayerhq.hu>`_ suite
439+
for this::
440+
441+
442+
#fps (frames per second) controls the play speed
443+
mencoder 'mf://*.png' -mf type=png:fps=10 -ovc \\
444+
lavc -lavcopts vcodec=wmv2 -oac copy -o animation.avi
445+
446+
The swiss army knife of image tools, ImageMagick's `convert
447+
<http://www.imagemagick.org/script/convert.php>`_ works for this as
448+
well.<p>
449+
450+
Here is a simple example script that saves some PNGs, makes them into
451+
a movie, and then cleans up::
452+
453+
import os, sys
454+
import matplotlib.pyplot as plt
455+
456+
files = []
457+
fig = plt.figure(figsize=(5,5))
458+
ax = fig.add_subplot(111)
459+
for i in range(50): # 50 frames
460+
ax.cla()
461+
ax.imshow(rand(5,5), interpolation='nearest')
462+
fname = '_tmp%03d.png'%i
463+
print 'Saving frame', fname
464+
fig.savefig(fname)
465+
files.append(fname)
466+
467+
print 'Making movie animation.mpg - this make take a while'
468+
os.system("mencoder 'mf://_tmp*.png' -mf type=png:fps=10 \\
469+
-ovc lavc -lavcopts vcodec=wmv2 -oac copy -o animation.mpg")
470+
471+
.. htmlonly::
472+
473+
See :ref:`animation-movie_demo` for a complete example.

doc/faq/installing_faq.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ and build and install as usual with::
105105
> cd matplotlib
106106
> python setup.py install
107107

108+
109+
108110
Backends
109111
========
110112

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,66 @@
33
# For detailed comments on animation and the techniques used here, see
44
# the wiki entry
55
# http://www.scipy.org/wikis/topical_software/MatplotlibAnimation
6-
import sys
76
import time
87

98
import gtk, gobject
109

1110
import matplotlib
1211
matplotlib.use('GTKAgg')
13-
import numpy as npy
14-
import pylab as p
1512

13+
import numpy as np
14+
import matplotlib.pyplot as plt
1615

17-
ax = p.subplot(111)
18-
canvas = ax.figure.canvas
1916

20-
p.subplots_adjust(left=0.3, bottom=0.3) # check for flipy bugs
21-
p.grid() # to ensure proper background restore
17+
fig = plt.figure()
18+
ax = fig.add_subplot(111)
19+
canvas = fig.canvas
20+
21+
fig.subplots_adjust(left=0.3, bottom=0.3) # check for flipy bugs
22+
ax.grid() # to ensure proper background restore
2223

2324
# create the initial line
24-
x = npy.arange(0,2*npy.pi,0.01)
25-
line, = p.plot(x, npy.sin(x), animated=True, lw=2)
25+
x = np.arange(0,2*np.pi,0.01)
26+
line, = ax.plot(x, np.sin(x), animated=True, lw=2)
27+
canvas.draw()
2628

2729
# for profiling
2830
tstart = time.time()
2931

3032
def update_line(*args):
33+
print 'you are here', update_line.cnt
3134
if update_line.background is None:
3235
update_line.background = canvas.copy_from_bbox(ax.bbox)
3336

3437
# restore the clean slate background
3538
canvas.restore_region(update_line.background)
3639
# update the data
37-
line.set_ydata(npy.sin(x+update_line.cnt/10.0))
40+
line.set_ydata(np.sin(x+update_line.cnt/10.0))
3841
# just draw the animated artist
39-
try:
40-
ax.draw_artist(line)
41-
except AssertionError:
42-
return
42+
ax.draw_artist(line)
43+
4344
# just redraw the axes rectangle
4445
canvas.blit(ax.bbox)
4546

4647
if update_line.cnt==1000:
4748
# print the timing info and quit
4849
print 'FPS:' , 1000/(time.time()-tstart)
49-
sys.exit()
50+
gtk.mainquit()
51+
raise SystemExit
5052

5153
update_line.cnt += 1
5254
return True
5355

5456
update_line.cnt = 0
5557
update_line.background = None
56-
gobject.idle_add(update_line)
57-
p.show()
58+
59+
60+
def start_anim(event):
61+
gobject.idle_add(update_line)
62+
canvas.mpl_disconnect(start_anim.cid)
63+
64+
start_anim.cid = canvas.mpl_connect('draw_event', start_anim)
65+
66+
67+
68+
plt.show()

examples/animation/movie_demo.py

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
#!/usr/bin/python
2+
#
3+
# Josh Lifton 2004
4+
#
5+
# Permission is hereby granted to use and abuse this document
6+
# so long as proper attribution is given.
7+
#
8+
# This Python script demonstrates how to use the numarray package
9+
# to generate and handle large arrays of data and how to use the
10+
# matplotlib package to generate plots from the data and then save
11+
# those plots as images. These images are then stitched together
12+
# by Mencoder to create a movie of the plotted data. This script
13+
# is for demonstration purposes only and is not intended to be
14+
# for general use. In particular, you will likely need to modify
15+
# the script to suit your own needs.
16+
#
17+
18+
19+
from matplotlib.matlab import * # For plotting graphs.
20+
import os # For issuing commands to the OS.
21+
import sys # For determining the Python version.
22+
23+
#
24+
# Print the version information for the machine, OS,
25+
# Python interpreter, and matplotlib. The version of
26+
# Mencoder is printed when it is called.
27+
#
28+
# This script is known to have worked for:
29+
#
30+
# OS version: ('Linux', 'flux-capacitor', '2.4.26', '#1 SMP Sa Apr 17 19:33:42 CEST 2004', 'i686')
31+
# Python version: 2.3.4 (#2, May 29 2004, 03:31:27) [GCC 3.3.3 (Debian 20040417)]
32+
# matplotlib version: 0.61.0
33+
# MEncoder version:
34+
# MEncoder 1.0pre4-3.3.3 (C) 2000-2004 MPlayer Team
35+
# CPU: Intel Celeron 2/Pentium III Coppermine,Geyserville 996.1 MHz (Family: 6, Stepping: 10)
36+
# Detected cache-line size is 32 bytes
37+
# CPUflags: Type: 6 MMX: 1 MMX2: 1 3DNow: 0 3DNow2: 0 SSE: 1 SSE2: 0
38+
# Compiled for x86 CPU with extensions: MMX MMX2 SSE
39+
#
40+
print 'Executing on', os.uname()
41+
print 'Python version', sys.version
42+
print 'matplotlib version', matplotlib.__version__
43+
44+
45+
46+
#
47+
# First, let's create some data to work with. In this example
48+
# we'll use a normalized Gaussian waveform whose mean and
49+
# standard deviation both increase linearly with time. Such a
50+
# waveform can be thought of as a propagating system that loses
51+
# coherence over time, as might happen to the probability
52+
# distribution of a clock subjected to independent, identically
53+
# distributed Gaussian noise at each time step.
54+
#
55+
56+
print 'Initializing data set...' # Let the user know what's happening.
57+
58+
# Initialize variables needed to create and store the example data set.
59+
numberOfTimeSteps = 100 # Number of frames we want in the movie.
60+
x = arange(-10,10,0.01) # Values to be plotted on the x-axis.
61+
mean = -6 # Initial mean of the Gaussian.
62+
stddev = 0.2 # Initial standard deviation.
63+
meaninc = 0.1 # Mean increment.
64+
stddevinc = 0.1 # Standard deviation increment.
65+
66+
# Create an array of zeros and fill it with the example data.
67+
y = zeros((numberOfTimeSteps,len(x)), Float64)
68+
for i in range(numberOfTimeSteps) :
69+
y[i] = (1/sqrt(2*pi*stddev))*exp(-((x-mean)**2)/(2*stddev))
70+
mean = mean + meaninc
71+
stddev = stddev + stddevinc
72+
73+
print 'Done.' # Let the user know what's happening.
74+
75+
#
76+
# Now that we have an example data set (x,y) to work with, we can
77+
# start graphing it and saving the images.
78+
#
79+
80+
for i in range(len(y)) :
81+
#
82+
# The next four lines are just like Matlab.
83+
#
84+
plot(x,y[i],'b.')
85+
axis((x[0],x[-1],-0.25,1))
86+
xlabel('time (ms)')
87+
ylabel('probability density function')
88+
89+
#
90+
# Notice the use of LaTeX-like markup.
91+
#
92+
title(r'$\cal{N}(\mu, \sigma^2)$', fontsize=20)
93+
94+
#
95+
# The file name indicates how the image will be saved and the
96+
# order it will appear in the movie. If you actually wanted each
97+
# graph to be displayed on the screen, you would include commands
98+
# such as show() and draw() here. See the matplotlib
99+
# documentation for details. In this case, we are saving the
100+
# images directly to a file without displaying them.
101+
#
102+
filename = str('%03d' % i) + '.png'
103+
savefig(filename, dpi=100)
104+
105+
#
106+
# Let the user know what's happening.
107+
#
108+
print 'Wrote file', filename
109+
110+
#
111+
# Clear the figure to make way for the next image.
112+
#
113+
clf()
114+
115+
#
116+
# Now that we have graphed images of the dataset, we will stitch them
117+
# together using Mencoder to create a movie. Each image will become
118+
# a single frame in the movie.
119+
#
120+
# We want to use Python to make what would normally be a command line
121+
# call to Mencoder. Specifically, the command line call we want to
122+
# emulate is (without the initial '#'):
123+
# mencoder mf://*.png -mf type=png:w=800:h=600:fps=25 -ovc lavc -lavcopts vcodec=mpeg4 -oac copy -o output.avi
124+
# See the MPlayer and Mencoder documentation for details.
125+
#
126+
127+
command = ('mencoder',
128+
'mf://*.png',
129+
'-mf',
130+
'type=png:w=800:h=600:fps=25',
131+
'-ovc',
132+
'lavc',
133+
'-lavcopts',
134+
'vcodec=mpeg4',
135+
'-oac',
136+
'copy',
137+
'-o',
138+
'output.avi')
139+
140+
os.spawnvp(os.P_WAIT, 'mencoder', command)

0 commit comments

Comments
 (0)