One-dimensional interpolation estimates values between ordered measurements. In SciPy, a callable interpolator lets Python code evaluate in-between positions while keeping the original x and y samples separate from later query points.
Current scipy.interpolate routines use dedicated objects for different curve shapes. PchipInterpolator fits monotonic measured data with a shape-preserving cubic curve, which avoids the legacy interp1d interface and reduces overshoot compared with a standard cubic spline.
Use strictly increasing x values with no duplicates, and decide how the code should handle query points outside the measured range. Disabling extrapolation with extrapolate=False returns NaN outside the interval instead of extending the first or last curve segment.
Related: Fit a curve to noisy data
import numpy as np from scipy.interpolate import PchipInterpolator x = np.array([0.0, 10.0, 20.0, 30.0, 40.0]) y = np.array([0.2, 1.1, 1.8, 2.4, 2.7]) interpolator = PchipInterpolator(x, y, extrapolate=False) x_query = np.array([5.0, 15.0, 25.0, 35.0]) y_query = interpolator(x_query) print("Interpolated signal:") for xi, yi in zip(x_query, y_query): print(f"x={xi:4.1f} -> y={yi:.3f}") print() print("Known sample points match:", np.allclose(interpolator(x), y)) print("Out-of-range values:", interpolator(np.array([-5.0, 45.0])))
PchipInterpolator requires x values to increase with no duplicates. Sort the input data and combine duplicate samples before constructing the interpolator.
$ python3 interpolate_1d.py Interpolated signal: x= 5.0 -> y=0.677 x=15.0 -> y=1.468 x=25.0 -> y=2.131 x=35.0 -> y=2.581 Known sample points match: True Out-of-range values: [nan nan]
x = np.array([0.0, 10.0, 20.0, 30.0, 40.0]) y = np.array([0.2, 1.1, 1.8, 2.4, 2.7])
The y length along the interpolation axis must match the x length.
interpolator = PchipInterpolator(x, y, extrapolate=False)
Use extrapolate=True only when values outside the measured interval should follow the edge curve segment.
x_query = np.array([5.0, 15.0, 25.0, 35.0]) y_query = interpolator(x_query)
print("Known sample points match:", np.allclose(interpolator(x), y))
print("Out-of-range values:", interpolator(np.array([-5.0, 45.0])))
The output should be [nan nan] when extrapolate=False and both query values are outside the measured x range.
$ rm interpolate_1d.py