SCRIPTS INTERPOLATION interp_2DΒΆ

# -*-Python-*-
# Created by bgrierson at 02 Sep 2017  06:30

"""
This script demonstrates 2D interpolation methods

"""

nx = 21
ny = 31
x = linspace(0, 2 * np.pi, nx)
y = linspace(0, 1, ny)
xx, yy = meshgrid(x, y)
z = np.sin(yy - xx)

x2 = linspace(0, 2 * np.pi, 21)
y2 = linspace(-0.1, 1.1, 21)
xx2, yy2 = meshgrid(x2, y2)

# Use RegularGridInterpolator since z[i,j] = z(x[i], y[j]) on a regular grid
ifun = RegularGridInterpolator((x, y), z.T, bounds_error=False)
z2 = ifun((xx2, yy2))

# Use griddata as if z[i,j] = z(x[i,j], y[i,j]) on an arbitrary irregular grid.
xx1 = xx.flatten()
yy1 = yy.flatten()
z1 = z.flatten()
z3 = griddata((xx1, yy1), z1, (xx2, yy2))

# Use LinearNDInterpolator as if z[i,j] = z(x[i,j], y[i,j]) on an arbitrary irregular grid.
ifun = LinearNDInterpolator(np.array([xx1, yy1]).T, z1)
z4 = ifun((xx2, yy2))

fig, ax = plt.subplots(nrows=2, ncols=2)
ax[0, 0].contourf(x, y, z)
ax[0, 0].plot(xx, yy, marker='o', color='black', alpha=0.2)
ax[0, 0].set_title('Original')
ax[0, 1].contourf(x2, y2, z2)
ax[0, 1].set_title('Interpolated including out-of-bounds')
ax[1, 0].contourf(x2, y2, z3)
ax[1, 1].contourf(x2, y2, z4)