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

Skip to content

Commit 4d20a2f

Browse files
committed
Update movie_demo.py to work with current mpl.
svn path=/trunk/matplotlib/; revision=6314
1 parent 7f80a76 commit 4d20a2f

File tree

1 file changed

+43
-27
lines changed

1 file changed

+43
-27
lines changed

examples/animation/movie_demo.py

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,40 @@
1515
# the script to suit your own needs.
1616
#
1717

18-
19-
from matplotlib.matlab import * # For plotting graphs.
20-
import os # For issuing commands to the OS.
18+
import matplotlib
19+
matplotlib.use('Agg')
20+
import matplotlib.pyplot as plt # For plotting graphs.
21+
import numpy as np
22+
import subprocess # For issuing commands to the OS.
23+
import os
2124
import sys # For determining the Python version.
2225

2326
#
2427
# Print the version information for the machine, OS,
2528
# Python interpreter, and matplotlib. The version of
2629
# Mencoder is printed when it is called.
2730
#
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-
#
4031
print 'Executing on', os.uname()
4132
print 'Python version', sys.version
4233
print 'matplotlib version', matplotlib.__version__
4334

35+
not_found_msg = """
36+
The mencoder command was not found;
37+
mencoder is used by this script to make an avi file from a set of pngs.
38+
It is typically not installed by default on linux distros because of
39+
legal restrictions, but it is widely available.
40+
"""
41+
42+
try:
43+
subprocess.check_call(['mencoder'])
44+
except subprocess.CalledProcessError:
45+
print "mencoder command was found"
46+
pass # mencoder is found, but returns non-zero exit as expected
47+
# This is a quick and dirty check; it leaves some spurious output
48+
# for the user to puzzle over.
49+
except OSError:
50+
print not_found_msg
51+
sys.exit("quitting\n")
4452

4553

4654
#
@@ -57,16 +65,16 @@
5765

5866
# Initialize variables needed to create and store the example data set.
5967
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.
68+
x = np.arange(-10,10,0.01) # Values to be plotted on the x-axis.
6169
mean = -6 # Initial mean of the Gaussian.
6270
stddev = 0.2 # Initial standard deviation.
6371
meaninc = 0.1 # Mean increment.
6472
stddevinc = 0.1 # Standard deviation increment.
6573

6674
# Create an array of zeros and fill it with the example data.
67-
y = zeros((numberOfTimeSteps,len(x)), Float64)
75+
y = np.zeros((numberOfTimeSteps,len(x)), float)
6876
for i in range(numberOfTimeSteps) :
69-
y[i] = (1/sqrt(2*pi*stddev))*exp(-((x-mean)**2)/(2*stddev))
77+
y[i] = (1/np.sqrt(2*np.pi*stddev))*np.exp(-((x-mean)**2)/(2*stddev))
7078
mean = mean + meaninc
7179
stddev = stddev + stddevinc
7280

@@ -81,15 +89,15 @@
8189
#
8290
# The next four lines are just like Matlab.
8391
#
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-
92+
plt.plot(x,y[i],'b.')
93+
plt.axis((x[0],x[-1],-0.25,1))
94+
plt.xlabel('time (ms)')
95+
plt.ylabel('probability density function')
96+
8997
#
9098
# Notice the use of LaTeX-like markup.
9199
#
92-
title(r'$\cal{N}(\mu, \sigma^2)$', fontsize=20)
100+
plt.title(r'$\cal{N}(\mu, \sigma^2)$', fontsize=20)
93101

94102
#
95103
# The file name indicates how the image will be saved and the
@@ -100,7 +108,7 @@
100108
# images directly to a file without displaying them.
101109
#
102110
filename = str('%03d' % i) + '.png'
103-
savefig(filename, dpi=100)
111+
plt.savefig(filename, dpi=100)
104112

105113
#
106114
# Let the user know what's happening.
@@ -110,7 +118,7 @@
110118
#
111119
# Clear the figure to make way for the next image.
112120
#
113-
clf()
121+
plt.clf()
114122

115123
#
116124
# Now that we have graphed images of the dataset, we will stitch them
@@ -137,4 +145,12 @@
137145
'-o',
138146
'output.avi')
139147

140-
os.spawnvp(os.P_WAIT, 'mencoder', command)
148+
#os.spawnvp(os.P_WAIT, 'mencoder', command)
149+
150+
print "\n\nabout to execute:\n%s\n\n" % ' '.join(command)
151+
subprocess.check_call(command)
152+
153+
print "\n\n The movie was written to 'output.avi'"
154+
155+
print "\n\n You may want to delete *.png now.\n\n"
156+

0 commit comments

Comments
 (0)