interp1d
- class scipy.interpolate.interp1d(x, y, kind='linear', axis=-1, copy=True, bounds_error=None, fill_value=nan, assume_sorted
Interpolate a 1-D function (legacy).
x and y are arrays of values used to approximate some function f:
y = f(x). This class returns a function whose call method uses
interpolation to find the value of new points.
A 1-D array of real values.
An N-D array of real values. The length of y along the interpolation
axis must be equal to the length of x. Use the axis parameter
to select correct axis. Unlike other interpolators, the default
interpolation axis is the last axis of y.
Specifies the kind of interpolation as a string or as an integer specifying the order of the spline interpolator to use. The string has to be one of ‘linear’, ‘nearest’, ‘nearest-up’, ‘zero’, ‘slinear’, ‘quadratic’, ‘cubic’, ‘previous’, or ‘next’. ‘zero’, ‘slinear’, ‘quadratic’ and ‘cubic’ refer to a spline interpolation of zeroth, first, second or third order; ‘previous’ and ‘next’ simply return the previous or next value of the point; ‘nearest-up’ and ‘nearest’ differ when interpolating half-integers (e.g. 0.5, 1.5) in that ‘nearest-up’ rounds up and ‘nearest’ rounds down. Default is ‘linear’.
Axis in the y array corresponding to the x-coordinate values. Unlike
other interpolators, defaults to axis=-1.
If True, the class makes internal copies of x and y. If False,
references to x and y are used if possible. The default is to copy.
If True, a ValueError is raised any time interpolation is attempted on
a value outside of the range of x (where extrapolation is
necessary). If False, out of bounds values are assigned fill_value.
By default, an error is raised unless fill_value="extrapolate".
if an ndarray (or float), this value will be used to fill in for requested points outside of the data range. If not provided, then the default is NaN. The array-like must broadcast properly to the dimensions of the non-interpolation axes.
If a two-element tuple, then the first element is used as a
fill value for x_new < x[0] and the second element is used for
x_new > x[-1]. Anything that is not a 2-element tuple (e.g.,
list or ndarray, regardless of shape) is taken to be a single
array-like argument meant to be used for both bounds as
below, above = fill_value, fill_value. Using a two-element tuple
or ndarray requires bounds_error=False.
If “extrapolate”, then points outside the data range will be extrapolated.
If False, values of x can be in any order and they are sorted first. If True, x has to be an array of monotonically increasing values.
fill_valueThe fill value.
| Evaluate the interpolant |
Calling interp1d with NaNs present in input values results in
undefined behaviour.
Input values x and y must be convertible to float values like int or float.
If the values in x are not unique, the resulting behavior is undefined and specific to the choice of kind, i.e., changing kind will change the behavior for duplicates.
Array API Standard Support
interp1d is not in-scope for support of Python Array API Standard compatible
backends other than NumPy.
See Support for the array API standard for more information.
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> from scipy import interpolate
>>> x = np.arange(0, 10)
>>>
2-D interpolation
>>> xnew = np.arange(0, 9, 0.1)
>>> ynew = f(xnew) # use interpolation function returned by `interp1d`
>>> plt.plot(x, y, 'o', xnew, ynew, '-')
>>> plt.show()