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

Skip to content

Commit 921021c

Browse files
committed
cleaned some more examples
svn path=/trunk/matplotlib/; revision=5683
1 parent c28e218 commit 921021c

7 files changed

Lines changed: 97 additions & 91 deletions

File tree

examples/pylab_examples/cohere_demo.py

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,35 @@
22
"""
33
Compute the coherence of two signals
44
"""
5-
import numpy as n
5+
import numpy as np
6+
import matplotlib.pyplot as plt
67

7-
from pylab import figure, show
8+
# make a little extra space between the subplots
9+
plt.subplots_adjust(wspace=0.5)
810

911
dt = 0.01
10-
t = n.arange(0, 30, dt)
11-
Nt = len(t)
12-
nse1 = n.random.randn(Nt) # white noise 1
13-
nse2 = n.random.randn(Nt) # white noise 2
14-
r = n.exp(-t/0.05)
12+
t = np.arange(0, 30, dt)
13+
nse1 = np.random.randn(len(t)) # white noise 1
14+
nse2 = np.random.randn(len(t)) # white noise 2
15+
r = np.exp(-t/0.05)
1516

16-
cnse1 = n.convolve(nse1, r)*dt # colored noise 1
17-
cnse1 = cnse1[:Nt]
18-
cnse2 = n.convolve(nse2, r)*dt # colored noise 2
19-
cnse2 = cnse2[:Nt]
17+
cnse1 = np.convolve(nse1, r, mode='same')*dt # colored noise 1
18+
cnse2 = np.convolve(nse2, r, mode='same')*dt # colored noise 2
2019

2120
# two signals with a coherent part and a random part
22-
s1 = 0.01*n.sin(2*n.pi*10*t) + cnse1
23-
s2 = 0.01*n.sin(2*n.pi*10*t) + cnse2
24-
25-
fig = figure()
26-
ax = fig.add_subplot(211)
27-
ax.plot(t, s1, 'b-', t, s2, 'g-')
28-
ax.set_xlim(0,5)
29-
ax.set_xlabel('time')
30-
ax.set_ylabel('s1 and s2')
31-
32-
ax = fig.add_subplot(212)
33-
cxy, f = ax.cohere(s1, s2, 256, 1./dt)
34-
35-
show()
21+
s1 = 0.01*np.sin(2*np.pi*10*t) + cnse1
22+
s2 = 0.01*np.sin(2*np.pi*10*t) + cnse2
23+
24+
plt.subplot(211)
25+
plt.plot(t, s1, 'b-', t, s2, 'g-')
26+
plt.xlim(0,5)
27+
plt.xlabel('time')
28+
plt.ylabel('s1 and s2')
29+
plt.grid(True)
30+
31+
plt.subplot(212)
32+
cxy, f = plt.cohere(s1, s2, 256, 1./dt)
33+
plt.ylabel('coherence')
34+
plt.show()
3635

3736

examples/pylab_examples/csd_demo.py

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,35 @@
22
"""
33
Compute the cross spectral density of two signals
44
"""
5-
from __future__ import division
6-
from pylab import *
5+
import numpy as np
6+
import matplotlib.pyplot as plt
7+
8+
# make a little extra space between the subplots
9+
plt.subplots_adjust(wspace=0.5)
710

811
dt = 0.01
9-
t = arange(0, 30, dt)
10-
nse1 = randn(len(t)) # white noise 1
11-
nse2 = randn(len(t)) # white noise 2
12-
r = exp(divide(-t,0.05))
12+
t = np.arange(0, 30, dt)
13+
nse1 = np.random.randn(len(t)) # white noise 1
14+
nse2 = np.random.randn(len(t)) # white noise 2
15+
r = np.exp(-t/0.05)
1316

14-
cnse1 = convolve(nse1, r, mode=2)*dt # colored noise 1
15-
cnse1 = cnse1[:len(t)]
16-
cnse2 = convolve(nse2, r, mode=2)*dt # colored noise 2
17-
cnse2 = cnse2[:len(t)]
17+
cnse1 = np.convolve(nse1, r, mode='same')*dt # colored noise 1
18+
cnse2 = np.convolve(nse2, r, mode='same')*dt # colored noise 2
1819

1920
# two signals with a coherent part and a random part
20-
s1 = 0.01*sin(2*pi*10*t) + cnse1
21-
s2 = 0.01*sin(2*pi*10*t) + cnse2
22-
23-
subplot(211)
24-
plot(t, s1, 'b-', t, s2, 'g-')
25-
xlim(0,5)
26-
xlabel('time')
27-
ylabel('s1 and s2')
28-
29-
subplot(212)
30-
cxy, f = csd(s1, s2, 256, 1/dt)
31-
show()
21+
s1 = 0.01*np.sin(2*np.pi*10*t) + cnse1
22+
s2 = 0.01*np.sin(2*np.pi*10*t) + cnse2
23+
24+
plt.subplot(211)
25+
plt.plot(t, s1, 'b-', t, s2, 'g-')
26+
plt.xlim(0,5)
27+
plt.xlabel('time')
28+
plt.ylabel('s1 and s2')
29+
plt.grid(True)
30+
31+
plt.subplot(212)
32+
cxy, f = plt.csd(s1, s2, 256, 1./dt)
33+
plt.ylabel('CSD (db)')
34+
plt.show()
3235

3336

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#!/usr/bin/env python
2-
from pylab import *
3-
t = arange(0.0, 1.01, 0.01)
4-
s = sin(2*2*pi*t)
2+
import numpy as np
3+
import matplotlib.pyplot as plt
54

6-
fill(t, s*exp(-5*t), 'r')
7-
grid(True)
8-
show()
5+
t = np.arange(0.0, 1.01, 0.01)
6+
s = np.sin(2*2*np.pi*t)
7+
8+
plt.fill(t, s*np.exp(-5*t), 'r')
9+
plt.grid(True)
10+
plt.show()
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
'''
1+
"""
22
hexbin is an axes method or pyplot function that is essentially
33
a pcolor of a 2-D histogram with hexagonal cells. It can be
44
much more informative than a scatter plot; in the first subplot
55
below, try substituting 'scatter' for 'hexbin'.
6-
'''
6+
"""
77

8-
from matplotlib.pyplot import *
98
import numpy as np
10-
9+
import matplotlib.pyplot as plt
1110
n = 100000
1211
x = np.random.standard_normal(n)
1312
y = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n)
@@ -16,19 +15,20 @@
1615
ymin = y.min()
1716
ymax = y.max()
1817

19-
subplot(121)
20-
hexbin(x,y)
21-
axis([xmin, xmax, ymin, ymax])
22-
title("Hexagon binning")
23-
cb = colorbar()
18+
plt.subplots_adjust(hspace=0.5)
19+
plt.subplot(121)
20+
plt.hexbin(x,y)
21+
plt.axis([xmin, xmax, ymin, ymax])
22+
plt.title("Hexagon binning")
23+
cb = plt.colorbar()
2424
cb.set_label('counts')
2525

26-
subplot(122)
27-
hexbin(x,y,bins='log')
28-
axis([xmin, xmax, ymin, ymax])
29-
title("With a log color scale")
30-
cb = colorbar()
26+
plt.subplot(122)
27+
plt.hexbin(x,y,bins='log')
28+
plt.axis([xmin, xmax, ymin, ymax])
29+
plt.title("With a log color scale")
30+
cb = plt.colorbar()
3131
cb.set_label('log10(N)')
3232

33-
show()
33+
plt.show()
3434

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
#!/usr/bin/env python
2-
import pylab as P
2+
import numpy as np
3+
import matplotlib.mlab as mlab
4+
import matplotlib.pyplot as plt
35

46
mu, sigma = 100, 15
5-
x = mu + sigma*P.randn(10000)
7+
x = mu + sigma*np.random.randn(10000)
68

79
# the histogram of the data
8-
n, bins, patches = P.hist(x, 50, normed=1)
9-
P.setp(patches, 'facecolor', 'g', 'alpha', 0.75)
10+
n, bins, patches = plt.hist(x, 50, normed=1, facecolor='green', alpha=0.75)
1011

1112
# add a 'best fit' line
12-
y = P.normpdf( bins, mu, sigma)
13-
l = P.plot(bins, y, 'r--')
14-
P.setp(l, 'linewidth', 1)
13+
y = mlab.normpdf( bins, mu, sigma)
14+
l = plt.plot(bins, y, 'r--', linewidth=1)
1515

16-
P.xlabel('Smarts')
17-
P.ylabel('Probability')
18-
P.title(r'$\mathrm{Histogram\ of\ IQ:}\ \mu=100,\ \sigma=15$')
19-
P.axis([40, 160, 0, 0.03])
20-
P.grid(True)
16+
plt.xlabel('Smarts')
17+
plt.ylabel('Probability')
18+
plt.title(r'$\mathrm{Histogram\ of\ IQ:}\ \mu=100,\ \sigma=15$')
19+
plt.axis([40, 160, 0, 0.03])
20+
plt.grid(True)
2121

22-
#P.savefig('histogram_demo',dpi=72)
23-
P.show()
22+
plt.show()
Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
#!/usr/bin/env python
2-
from pylab import *
2+
import numpy as np
3+
import matplotlib.cm as cm
4+
import matplotlib.mlab as mlab
5+
import matplotlib.pyplot as plt
36

47
delta = 0.025
5-
x = y = arange(-3.0, 3.0, delta)
6-
X, Y = meshgrid(x, y)
7-
Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
8-
Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
8+
x = y = np.arange(-3.0, 3.0, delta)
9+
X, Y = np.meshgrid(x, y)
10+
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
11+
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
912
Z = Z2-Z1 # difference of Gaussians
1013

11-
im = imshow(Z, interpolation='bilinear', cmap=cm.gray,
12-
origin='lower', extent=[-3,3,-3,3])
14+
im = plt.imshow(Z, interpolation='bilinear', cmap=cm.gray,
15+
origin='lower', extent=[-3,3,-3,3])
1316

14-
savefig('image_demo')
15-
show()
17+
plt.show()
1618

lib/mpl_examples

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../examples

0 commit comments

Comments
 (0)