Skip to content

Commit

Permalink
cleaned some more examples
Browse files Browse the repository at this point in the history
svn path=/trunk/matplotlib/; revision=5683
  • Loading branch information
jdh2358 committed Jun 26, 2008
1 parent c28e218 commit 921021c
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 91 deletions.
49 changes: 24 additions & 25 deletions examples/pylab_examples/cohere_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,35 @@
"""
Compute the coherence of two signals
"""
import numpy as n
import numpy as np
import matplotlib.pyplot as plt

from pylab import figure, show
# make a little extra space between the subplots
plt.subplots_adjust(wspace=0.5)

dt = 0.01
t = n.arange(0, 30, dt)
Nt = len(t)
nse1 = n.random.randn(Nt) # white noise 1
nse2 = n.random.randn(Nt) # white noise 2
r = n.exp(-t/0.05)
t = np.arange(0, 30, dt)
nse1 = np.random.randn(len(t)) # white noise 1
nse2 = np.random.randn(len(t)) # white noise 2
r = np.exp(-t/0.05)

cnse1 = n.convolve(nse1, r)*dt # colored noise 1
cnse1 = cnse1[:Nt]
cnse2 = n.convolve(nse2, r)*dt # colored noise 2
cnse2 = cnse2[:Nt]
cnse1 = np.convolve(nse1, r, mode='same')*dt # colored noise 1
cnse2 = np.convolve(nse2, r, mode='same')*dt # colored noise 2

# two signals with a coherent part and a random part
s1 = 0.01*n.sin(2*n.pi*10*t) + cnse1
s2 = 0.01*n.sin(2*n.pi*10*t) + cnse2

fig = figure()
ax = fig.add_subplot(211)
ax.plot(t, s1, 'b-', t, s2, 'g-')
ax.set_xlim(0,5)
ax.set_xlabel('time')
ax.set_ylabel('s1 and s2')

ax = fig.add_subplot(212)
cxy, f = ax.cohere(s1, s2, 256, 1./dt)

show()
s1 = 0.01*np.sin(2*np.pi*10*t) + cnse1
s2 = 0.01*np.sin(2*np.pi*10*t) + cnse2

plt.subplot(211)
plt.plot(t, s1, 'b-', t, s2, 'g-')
plt.xlim(0,5)
plt.xlabel('time')
plt.ylabel('s1 and s2')
plt.grid(True)

plt.subplot(212)
cxy, f = plt.cohere(s1, s2, 256, 1./dt)
plt.ylabel('coherence')
plt.show()


47 changes: 25 additions & 22 deletions examples/pylab_examples/csd_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,35 @@
"""
Compute the cross spectral density of two signals
"""
from __future__ import division
from pylab import *
import numpy as np
import matplotlib.pyplot as plt

# make a little extra space between the subplots
plt.subplots_adjust(wspace=0.5)

dt = 0.01
t = arange(0, 30, dt)
nse1 = randn(len(t)) # white noise 1
nse2 = randn(len(t)) # white noise 2
r = exp(divide(-t,0.05))
t = np.arange(0, 30, dt)
nse1 = np.random.randn(len(t)) # white noise 1
nse2 = np.random.randn(len(t)) # white noise 2
r = np.exp(-t/0.05)

cnse1 = convolve(nse1, r, mode=2)*dt # colored noise 1
cnse1 = cnse1[:len(t)]
cnse2 = convolve(nse2, r, mode=2)*dt # colored noise 2
cnse2 = cnse2[:len(t)]
cnse1 = np.convolve(nse1, r, mode='same')*dt # colored noise 1
cnse2 = np.convolve(nse2, r, mode='same')*dt # colored noise 2

# two signals with a coherent part and a random part
s1 = 0.01*sin(2*pi*10*t) + cnse1
s2 = 0.01*sin(2*pi*10*t) + cnse2

subplot(211)
plot(t, s1, 'b-', t, s2, 'g-')
xlim(0,5)
xlabel('time')
ylabel('s1 and s2')

subplot(212)
cxy, f = csd(s1, s2, 256, 1/dt)
show()
s1 = 0.01*np.sin(2*np.pi*10*t) + cnse1
s2 = 0.01*np.sin(2*np.pi*10*t) + cnse2

plt.subplot(211)
plt.plot(t, s1, 'b-', t, s2, 'g-')
plt.xlim(0,5)
plt.xlabel('time')
plt.ylabel('s1 and s2')
plt.grid(True)

plt.subplot(212)
cxy, f = plt.csd(s1, s2, 256, 1./dt)
plt.ylabel('CSD (db)')
plt.show()


14 changes: 8 additions & 6 deletions examples/pylab_examples/fill_demo.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/usr/bin/env python
from pylab import *
t = arange(0.0, 1.01, 0.01)
s = sin(2*2*pi*t)
import numpy as np
import matplotlib.pyplot as plt

fill(t, s*exp(-5*t), 'r')
grid(True)
show()
t = np.arange(0.0, 1.01, 0.01)
s = np.sin(2*2*np.pi*t)

plt.fill(t, s*np.exp(-5*t), 'r')
plt.grid(True)
plt.show()
30 changes: 15 additions & 15 deletions examples/pylab_examples/hexbin_demo.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
'''
"""
hexbin is an axes method or pyplot function that is essentially
a pcolor of a 2-D histogram with hexagonal cells. It can be
much more informative than a scatter plot; in the first subplot
below, try substituting 'scatter' for 'hexbin'.
'''
"""

from matplotlib.pyplot import *
import numpy as np

import matplotlib.pyplot as plt
n = 100000
x = np.random.standard_normal(n)
y = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n)
Expand All @@ -16,19 +15,20 @@
ymin = y.min()
ymax = y.max()

subplot(121)
hexbin(x,y)
axis([xmin, xmax, ymin, ymax])
title("Hexagon binning")
cb = colorbar()
plt.subplots_adjust(hspace=0.5)
plt.subplot(121)
plt.hexbin(x,y)
plt.axis([xmin, xmax, ymin, ymax])
plt.title("Hexagon binning")
cb = plt.colorbar()
cb.set_label('counts')

subplot(122)
hexbin(x,y,bins='log')
axis([xmin, xmax, ymin, ymax])
title("With a log color scale")
cb = colorbar()
plt.subplot(122)
plt.hexbin(x,y,bins='log')
plt.axis([xmin, xmax, ymin, ymax])
plt.title("With a log color scale")
cb = plt.colorbar()
cb.set_label('log10(N)')

show()
plt.show()

27 changes: 13 additions & 14 deletions examples/pylab_examples/histogram_demo.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
#!/usr/bin/env python
import pylab as P
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

mu, sigma = 100, 15
x = mu + sigma*P.randn(10000)
x = mu + sigma*np.random.randn(10000)

# the histogram of the data
n, bins, patches = P.hist(x, 50, normed=1)
P.setp(patches, 'facecolor', 'g', 'alpha', 0.75)
n, bins, patches = plt.hist(x, 50, normed=1, facecolor='green', alpha=0.75)

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

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

#P.savefig('histogram_demo',dpi=72)
P.show()
plt.show()
20 changes: 11 additions & 9 deletions examples/pylab_examples/image_demo.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#!/usr/bin/env python
from pylab import *
import numpy as np
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

delta = 0.025
x = y = arange(-3.0, 3.0, delta)
X, Y = meshgrid(x, y)
Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
x = y = np.arange(-3.0, 3.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = Z2-Z1 # difference of Gaussians

im = imshow(Z, interpolation='bilinear', cmap=cm.gray,
origin='lower', extent=[-3,3,-3,3])
im = plt.imshow(Z, interpolation='bilinear', cmap=cm.gray,
origin='lower', extent=[-3,3,-3,3])

savefig('image_demo')
show()
plt.show()

1 change: 1 addition & 0 deletions lib/mpl_examples

0 comments on commit 921021c

Please sign in to comment.