SciPy

scipy.interpolate.NearestNDInterpolator

class scipy.interpolate.NearestNDInterpolator(x, y)[source]

Nearest-neighbor interpolation in N dimensions.

New in version 0.9.

Parameters
x(Npoints, Ndims) ndarray of floats

Data point coordinates.

y(Npoints,) ndarray of float or complex

Data values.

rescaleboolean, optional

Rescale points to unit cube before performing interpolation. This is useful if some of the input dimensions have incommensurable units and differ by many orders of magnitude.

New in version 0.14.0.

tree_optionsdict, optional

Options passed to the underlying cKDTree.

New in version 0.17.0.

See also

griddata

Interpolate unstructured D-D data.

LinearNDInterpolator

Piecewise linear interpolant in N dimensions.

CloughTocher2DInterpolator

Piecewise cubic, C1 smooth, curvature-minimizing interpolant in 2D.

Notes

Uses scipy.spatial.cKDTree

Examples

We can interpolate values on a 2D plane:

>>> from scipy.interpolate import NearestNDInterpolator
>>> import matplotlib.pyplot as plt
>>> np.random.seed(0)
>>> x = np.random.random(10) - 0.5
>>> y = np.random.random(10) - 0.5
>>> z = np.hypot(x, y)
>>> X = np.linspace(min(x), max(x))
>>> Y = np.linspace(min(y), max(y))
>>> X, Y = np.meshgrid(X, Y)  # 2D grid for interpolation
>>> interp = NearestNDInterpolator(list(zip(x, y)), z)
>>> Z = interp(X, Y)
>>> plt.pcolormesh(X, Y, Z, shading='auto')
>>> plt.plot(x, y, "ok", label="input point")
>>> plt.legend()
>>> plt.colorbar()
>>> plt.axis("equal")
>>> plt.show()
../_images/scipy-interpolate-NearestNDInterpolator-1.png

Methods

__call__(self, *args)

Evaluate interpolator at given points.

Previous topic

scipy.interpolate.LinearNDInterpolator.__call__

Next topic

scipy.interpolate.NearestNDInterpolator.__call__