Note
Go to the end to download the full example code
2-Dimensional InterpolationΒΆ
6 import ndsplines
7 import numpy as np
8 import matplotlib.pyplot as plt
9 from scipy import interpolate
10 from scipy.stats import norm
11 import itertools
12 from mpl_toolkits.mplot3d import Axes3D
13
14
15 def gaussian(x_in):
16 z = norm.ppf(.995)
17 x = z*(2*x_in-1)
18 return norm.pdf(x)
19
20 def sin(x_in):
21 x = np.pi*(x_in-0.5)
22 return np.sin(x)
23
24 def tanh(x_in):
25 x = 2*np.pi*(x_in-0.5)
26 return np.tanh(x)
27
28 def dist(x_in, y_in):
29 return np.sqrt((x_in-0.25)**2 + (y_in-0.25)**2)
30
31 funcs = [gaussian, sin, tanh]
32
33 def wrap2d(funcx, funcy):
34 def func2d(x_in, y_in):
35 return funcx(x_in)*funcy(y_in)
36 func2d.__name__ = '_'.join([funcx.__name__, funcy.__name__])
37 return func2d
38
39 funcs = [ wrap2d(*funcs_to_wrap) for funcs_to_wrap in itertools.combinations_with_replacement(funcs, r=2)]
40 funcs.append(dist)
41
42 x = np.linspace(0, 1, 7)
43 y = np.linspace(0, 1, 7)
44
45 xx = np.linspace(0,1,64)
46 yy = np.linspace(0,1,64)
47
48 xx = np.linspace(-.25, 1.25, 64)
49 yy = np.linspace(-.25, 1.25, 64)
50 k = 3
51
52
53 meshx, meshy = np.meshgrid(x, y, indexing='ij')
54 gridxy = np.stack((meshx, meshy), axis=-1)
55
56
57 meshxx, meshyy = np.meshgrid(xx, yy, indexing='ij')
58 gridxxyy = np.stack((meshxx, meshyy), axis=-1)
59
60 for func in funcs:
61 fvals = func(meshx, meshy)
62 truef = func(meshxx, meshyy)
63 test_NDBspline = ndsplines.make_interp_spline(gridxy, fvals,)
64 test_RectSpline = interpolate.RectBivariateSpline(x, y, fvals)
65
66 fig = plt.figure()
67 ax = fig.add_subplot(111, projection='3d')
68
69 ax.plot_wireframe(meshxx, meshyy, truef, alpha=0.25, color='C0')
70 ax.plot_wireframe(meshxx, meshyy, test_NDBspline(gridxxyy), color='C1')
71 ax.plot_wireframe(meshxx, meshyy, test_RectSpline(meshxx, meshyy, grid=False), color='C2')
72 plt.show()
Total running time of the script: (0 minutes 1.435 seconds)