|
15 | 15 | # the script to suit your own needs. |
16 | 16 | # |
17 | 17 |
|
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 |
21 | 24 | import sys # For determining the Python version. |
22 | 25 |
|
23 | 26 | # |
24 | 27 | # Print the version information for the machine, OS, |
25 | 28 | # Python interpreter, and matplotlib. The version of |
26 | 29 | # Mencoder is printed when it is called. |
27 | 30 | # |
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 | 31 | print 'Executing on', os.uname() |
41 | 32 | print 'Python version', sys.version |
42 | 33 | print 'matplotlib version', matplotlib.__version__ |
43 | 34 |
|
| 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") |
44 | 52 |
|
45 | 53 |
|
46 | 54 | # |
|
57 | 65 |
|
58 | 66 | # Initialize variables needed to create and store the example data set. |
59 | 67 | 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. |
61 | 69 | mean = -6 # Initial mean of the Gaussian. |
62 | 70 | stddev = 0.2 # Initial standard deviation. |
63 | 71 | meaninc = 0.1 # Mean increment. |
64 | 72 | stddevinc = 0.1 # Standard deviation increment. |
65 | 73 |
|
66 | 74 | # 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) |
68 | 76 | 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)) |
70 | 78 | mean = mean + meaninc |
71 | 79 | stddev = stddev + stddevinc |
72 | 80 |
|
|
81 | 89 | # |
82 | 90 | # The next four lines are just like Matlab. |
83 | 91 | # |
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 | + |
89 | 97 | # |
90 | 98 | # Notice the use of LaTeX-like markup. |
91 | 99 | # |
92 | | - title(r'$\cal{N}(\mu, \sigma^2)$', fontsize=20) |
| 100 | + plt.title(r'$\cal{N}(\mu, \sigma^2)$', fontsize=20) |
93 | 101 |
|
94 | 102 | # |
95 | 103 | # The file name indicates how the image will be saved and the |
|
100 | 108 | # images directly to a file without displaying them. |
101 | 109 | # |
102 | 110 | filename = str('%03d' % i) + '.png' |
103 | | - savefig(filename, dpi=100) |
| 111 | + plt.savefig(filename, dpi=100) |
104 | 112 |
|
105 | 113 | # |
106 | 114 | # Let the user know what's happening. |
|
110 | 118 | # |
111 | 119 | # Clear the figure to make way for the next image. |
112 | 120 | # |
113 | | - clf() |
| 121 | + plt.clf() |
114 | 122 |
|
115 | 123 | # |
116 | 124 | # Now that we have graphed images of the dataset, we will stitch them |
|
137 | 145 | '-o', |
138 | 146 | 'output.avi') |
139 | 147 |
|
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