forked from scattering/dataflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
regular_gridding.py
93 lines (75 loc) · 2.45 KB
/
regular_gridding.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
89
90
91
92
93
import numpy as N
from matplotlib.mlab import griddata
import matplotlib.pyplot as plt
import numpy.ma as ma
from numpy.random import uniform
def regularlyGrid(xarr, yarr, zarr, xstart, xfinal, xstep, ystart, yfinal, ystep):
"Returns the regularly grided xi, yi, and zi arrays from the initial data."
# define grid.
xi = N.arange(xstart, xfinal, xstep)
yi = N.arange(ystart, yfinal, ystep)
'''
Programming note:
linspace does (start, final, how many steps)
arange does (start, final, step)
'''
# grid the data.
zi = griddata(xarr, yarr, zarr, xi, yi)
# contour the gridded data, plotting dots at the randomly spaced data points.
CS = plt.contour(xi,yi,zi,15,linewidths=0.5,colors='k')
CS = plt.contourf(xi,yi,zi,15,cmap=plt.cm.jet)
plt.colorbar() # draw colorbar
# plot data points.
plt.scatter(xarr, yarr, marker='o', c='b', s=5)
plt.xlim(xstart, xfinal) #setting xlimits
plt.ylim(ystart, yfinal) #setting ylimits
plt.title('Regular Grid')
plt.show()
return xi, yi, zi
def regularlyGridRandom():
"Makes a contour and contourf plot of randomly generated data pts."
# make up some randomly distributed data
npts = 100
x = uniform(-3,3,npts)
y = uniform(-3,3,npts)
z = x*N.exp(-x**2-y**2)
# define grid.
xi = N.arange(-3.1,3.1,0.05)
yi = N.arange(-3.1,3.1,0.05)
# grid the data.
zi = griddata(x,y,z,xi,yi)
# contour the gridded data, plotting dots at the randomly spaced data points.
CS = plt.contour(xi,yi,zi,15,linewidths=0.5,colors='k')
CS = plt.contourf(xi,yi,zi,15,cmap=plt.cm.jet)
plt.colorbar() # draw colorbar
# plot data points.
plt.scatter(x,y,marker='o',c='b',s=5)
plt.xlim(-3,3)
plt.ylim(-3,3)
plt.title('griddata test (%d points)' % npts)
plt.show()
def pcolorRandom():
"Makes a pcolormesh plot of randomly generated data pts."
# make up some randomly distributed data
npts = 100
x = uniform(-3,3,npts)
y = uniform(-3,3,npts)
z = x*N.exp(-x**2-y**2)
# define grid.
xi = N.arange(-3.1,3.1,0.05)
yi = N.arange(-3.1,3.1,0.05)
# grid the data.
zi = griddata(x,y,z,xi,yi)
# contour the gridded data, plotting dots at the randomly spaced data points.
plt.pcolormesh(xi, yi, zi)
#CS = plt.contour(xi,yi,zi,15,linewidths=0.5,colors='k')
#CS = plt.contourf(xi,yi,zi,15,cmap=plt.cm.jet)
plt.colorbar() # draw colorbar
# plot data points.
plt.scatter(x,y,marker='o',c='b',s=5)
plt.xlim(-3,3)
plt.ylim(-3,3)
plt.title('griddata test (%d points)' % npts)
plt.show()
if __name__=="__main__":
regularlyGridRandom()