/
mpl_pylab_examples.py
88 lines (71 loc) · 1.6 KB
/
mpl_pylab_examples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from matplotlib.pylab import *
raw_input=lambda x: None
def f(t):
return t**2*exp(-t**2)
t = linspace(0, 3, 51) # 51 points between 0 and 3
y = zeros(len(t)) # allocate y with float elements
for i in xrange(len(t)):
y[i] = f(t[i])
plot(t, y)
show()
raw_input('Press the Return key to quit: ')
# vectorized version:
y = f(t) # compute all f values at once
# or
y = t**2*exp(-t**2)
plot(t, y)
savefig('tmp1.pdf') # produce PDF
savefig('tmp1.png') # produce PNG
raw_input('Press the Return key to quit: ')
show()
plot(t, y)
legend(['t^2*exp(-t^2)'])
xlabel('t')
ylabel('y')
axis([0, 3, -0.05, 0.6]) # [tmin, tmax, ymin, ymax]
title('My First Matplotlib Demo')
savefig('tmp2.pdf') # produce PDF
show()
raw_input('Press the Return key to quit: ')
# Multiple curves
def f1(t):
return t**2*exp(-t**2)
def f2(t):
return t**2*f1(t)
t = linspace(0, 3, 51)
y1 = f1(t)
y2 = f2(t)
plot(t, y1, 'r-')
hold('on')
#hold(True)
plot(t, y2, 'bo')
xlabel('t')
ylabel('y')
legend(['t^2*exp(-t^2)', 't^4*exp(-t^2)'])
title('Plotting two curves in the same plot')
savefig('tmp3.pdf')
show()
# multiple plots
figure()
subplot(2, 1, 1)
t = linspace(0, 3, 51)
y1 = f1(t)
y2 = f2(t)
plot(t, y1, 'r-', t, y2, 'bo')
xlabel('t')
ylabel('y')
axis([t[0], t[-1], min(y2)-0.05, max(y2)+0.5])
legend(['t^2*exp(-t^2)', 't^4*exp(-t^2)'])
title('Top figure')
subplot(2, 1, 2)
t3 = t[::4]
y3 = f2(t3)
plot(t, y1, 'b-', t3, y3, 'ys')
legend(['t^2*exp(-t^2)', 't^4*exp(-t^2)'])
xlabel('t')
ylabel('y')
axis([0, 4, -0.2, 0.6])
title('Bottom figure')
savefig('tmp4.pdf')
show()
raw_input('Press the Return key to quit: ')