Skip to main content
`Ctrl`+`K`
scipy.ndimage.

# map_coordinates

scipy.ndimage.map_coordinates(input, coordinates, output=None, order=3, mode='constant', cval=0.0, prefilter=True)

  Map the input array to new coordinates by interpolation.

  The array of coordinates is used to find, for each point in the output, the corresponding coordinates in the input. The value of the input at those coordinates is determined by spline interpolation of the requested order.

  The shape of the output is derived from that of the coordinate array by dropping the first axis. The values of the array along the first axis are the coordinates in the input array at which the output value is found.

  Parameters:
    input array_like

      The input array.

    coordinates array_like

      The coordinates at which input is evaluated.

    output array or dtype, optional

      The array in which to place the output, or the dtype of the returned array. By default an array of the same dtype as input will be created.

    order int, optional

      The order of the spline interpolation, default is 3. The order has to be in the range 0-5.

    mode{‘reflect’, ‘grid-mirror’, ‘constant’, ‘grid-constant’, ‘nearest’, ‘mirror’, ‘grid-wrap’, ‘wrap’}, optional

      The mode parameter determines how the input array is extended beyond its boundaries. Default is ‘constant’. Behavior for each valid value is as follows (see additional plots and details on boundary modes):

      ‘reflect’ (d c b a | a b c d | d c b a)

        The input is extended by reflecting about the edge of the last pixel. This mode is also sometimes referred to as half-sample symmetric.

      ‘grid-mirror’

        This is a synonym for ‘reflect’.

      ‘constant’ (k k k k | a b c d | k k k k)

        The input is extended by filling all values beyond the edge with the same constant value, defined by the cval parameter. No interpolation is performed beyond the edges of the input.

      ‘grid-constant’ (k k k k | a b c d | k k k k)

        The input is extended by filling all values beyond the edge with the same constant value, defined by the cval parameter. Interpolation occurs for samples outside the input’s extent as well.

      ‘nearest’ (a a a a | a b c d | d d d d)

        The input is extended by replicating the last pixel.

      ‘mirror’ (d c b | a b c d | c b a)

        The input is extended by reflecting about the center of the last pixel. This mode is also sometimes referred to as whole-sample symmetric.

      ‘grid-wrap’ (a b c d | a b c d | a b c d)

        The input is extended by wrapping around to the opposite edge.

      ‘wrap’ (b c d b c | a b c d | b c a b c)

        The input is extended by wrapping around to the opposite edge, but in a way such that the last point and initial point exactly overlap. In this case it is not well defined which sample will be chosen at the point of overlap.

        For the example illustrated above, the edge values `a` and `d` overlap, while the inner pattern `b c` repeats. The example demonstrates one possible output, while `b c a b c | a b c d | b c d b c` is another, since either `a` or `d` may occur at each point of overlap.

    cval scalar, optional

      Value to fill past edges of input if mode is ‘constant’. Default is 0.0.

    prefilter bool, optional

      Determines if the input array is prefiltered with `spline_filter` before interpolation. The default is True, which will create a temporary float64 array of filtered values if `order > 1`. If setting this to False, the output will be slightly blurred if `order > 1`, unless the input is prefiltered, i.e. it is the result of calling `spline_filter` on the original input.

  Returns:
    map_coordinates ndarray

      The result of transforming the input. The shape of the output is derived from that of coordinates by dropping the first axis.

  See also

  `spline_filter`, `geometric_transform`, `scipy.interpolate`

  Notes

  For complex-valued input, this function maps the real and imaginary components independently.

  Added in version 1.6.0: Complex-valued support added.

  Array API Standard Support

  `map_coordinates` has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable `SCIPY_ARRAY_API=1` and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.

  Library | CPU | GPU
  --- | --- | ---
  NumPy | ✅ | n/a
  CuPy | n/a | ✅
  PyTorch | ✅ | ⛔
  JAX | ✅ | ✅
  Dask | ⚠️ computes graph | n/a

  See Support for the array API standard for more information.

  Examples

      >>> from scipy import ndimage
      >>> import numpy as np
      >>> a = np.arange(12.).reshape((4, 3))
      >>> a
      array([[  0.,   1.,   2.],
             [  3.,   4.,   5.],
             [  6.,   7.,   8.],
             [  9.,  10.,  11.]])
      >>> ndimage.map_coordinates(a, [[0.5, 2], [0.5, 1]], order=1)
      array([ 2.,  7.])

  Above, the interpolated value of a[0.5, 0.5] gives output[0], while a[2, 1] is output[1].

      >>> inds = np.array([[0.5, 2], [0.5, 4]])
      >>> ndimage.map_coordinates(a, inds, order=1, cval=-33.3)
      array([  2. , -33.3])
      >>> ndimage.map_coordinates(a, inds, order=1, mode='nearest')
      array([ 2.,  8.])
      >>> ndimage.map_coordinates(a, inds, order=1, cval=0, output=bool)
      array([ True, False], dtype=bool)