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.

Steps to interpolate one-dimensional data with SciPy PCHIP:

  1. Create a Python script named interpolate_1d.py.
    interpolate_1d.py
    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.

  2. Run the script and confirm the interpolated values.
    $ 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]
  3. Set the measured x and y arrays.
    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.

  4. Construct the PchipInterpolator with extrapolation disabled.
    interpolator = PchipInterpolator(x, y, extrapolate=False)

    Use extrapolate=True only when values outside the measured interval should follow the edge curve segment.

  5. Evaluate the interpolator at the query points.
    x_query = np.array([5.0, 15.0, 25.0, 35.0])
    y_query = interpolator(x_query)
  6. Verify that the original sample points still match.
    print("Known sample points match:", np.allclose(interpolator(x), y))
  7. Check how out-of-range values behave.
    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.

  8. Remove the sample script after adapting the pattern.
    $ rm interpolate_1d.py