File: example2.py

package info (click to toggle)
python-scipy 0.6.0-12
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 32,016 kB
  • ctags: 46,675
  • sloc: cpp: 124,854; ansic: 110,614; python: 108,664; fortran: 76,260; objc: 424; makefile: 384; sh: 10
file content (47 lines) | stat: -rw-r--r-- 1,139 bytes parent folder | download
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
from numpy import sin, asarray, exp, random, mgrid, pi, cos, sqrt, ones
from scipy.sandbox.rbf import Rbf
import pylab as pl

def truth_2d(x,y,w=2*pi):
    "moguls"
    return sin(w*x)*cos(w*y)

def truth_nd(*args):
    "a gausian sphere"
    x = asarray(list(args), 'float64')
    return exp( -sqrt((x**2).sum(axis=0)) )

# 2D example
N = 300
xi = random.rand(N)
yi = random.rand(N)
di = truth_2d(xi, yi)
xa, ya = mgrid[0:1:50j, 0:1:50j]
s = Rbf(xi, yi, di)
da = s(xa, ya)
pl.figure()
n = pl.normalize(-1., 1.)
pl.pcolor(xa, ya, da, norm=n, cmap=pl.cm.jet)
pl.scatter(xi, yi, 100, di, norm=n, cmap=pl.cm.jet)
pl.axis([0., 1., 0., 1.])
pl.colorbar()
pl.draw()
# 3d example
N = 300
xi = 2.*random.randn(N)
yi = 2.*random.randn(N)
zi = 2.*random.randn(N)
di = truth_nd(xi, yi, zi)
zas = [-0.25, 0.0, 0.25, 0.75]
xa, ya = mgrid[-1:1:50j, -1:1:50j]
s = Rbf(xi, yi, zi, di)
fig = pl.figure(figsize=(12, 3))
for idx, za in enumerate(zas):
    da = s(xa, ya, za*ones(xa.shape, 'f'))
    ax = fig.add_subplot(1,4,idx+1)
    ax.pcolor(xa, ya, da, norm=pl.normalize(0, 1), \
	  shading='flat', cmap=pl.cm.jet)
    ax.set_aspect('equal')

pl.show()