Smooth interpolation
Estimate values between measured samples while preserving smooth first and second derivatives.
SciPy · Function
CubicSpline builds a piecewise cubic interpolant through data samples with twice-continuous derivatives. It returns a callable PPoly representation for evaluating the fitted curve.
CubicSpline constructs a piecewise cubic polynomial through the supplied samples, choosing coefficients so the interpolant is twice continuously differentiable. It returns a PPoly object, which can be called at scalar or array-like coordinates and also supports polynomial operations documented by PPoly.
scipy.interpolate.CubicSpline(x, y, axis=0, bc_type='not-a-knot', extrapolate=None)| Name | Type | Description |
|---|---|---|
x | array_like | One-dimensional finite independent-variable coordinates in strictly increasing order; shape (n,). |
y | array_like | Finite dependent-variable samples. Its length along axis must equal len(x), and it may have any number of dimensions. |
axis | int, optional | Axis of y along which the samples vary; default is 0. |
bc_type | 2-tuple or str, optional | Boundary-condition specification for the two endpoints; default is 'not-a-knot'. |
extrapolate | {bool, 'periodic', None}, optional | Controls out-of-bounds evaluation: a Boolean, 'periodic', or None; default is None. |
A PPoly object representing the piecewise cubic interpolant.
import numpy as np
from scipy.interpolate import CubicSpline
x = np.array([0.0, 1.0, 2.0, 3.0])
y = np.array([0.0, 1.0, 0.0, 1.0])
spline = CubicSpline(x, y)
print(spline([0.5, 1.5, 2.5]))
Builds a cubic spline from finite, strictly increasing sample coordinates and evaluates it at new coordinates.
Estimate values between measured samples while preserving smooth first and second derivatives.
Interpolate multiple signals stored along one dimension of an array.
Evaluate a fitted curve and use PPoly capabilities such as derivatives or integrals.
Model cyclic measurements with matching endpoint behavior.
It creates a piecewise cubic interpolator that is twice continuously differentiable, and represents the result as a PPoly instance. The polynomial pieces use the input x values as breakpoints.
Follow-up: How can boundary behavior be changed? Use bc_type to specify endpoint derivative or value constraints, including built-in choices such as natural, clamped, and periodic.
x is a one-dimensional sequence of finite, strictly increasing independent-variable values. y contains finite dependent values, and its length along axis must equal the length of x.
Follow-up: What happens when y is multidimensional? The spline is constructed independently over the remaining dimensions while axis identifies the sample dimension.
extrapolate controls evaluation outside the data interval: a Boolean enables or disables extrapolation, while 'periodic' uses a periodic extension. With None, the behavior is selected from the boundary-condition setting.
Follow-up: Why might extrapolation be risky? The extrapolated values are based on the edge polynomial behavior rather than additional observed samples.
scipy.interpolate.PPolyscipy.interpolate.CubicHermiteSplinescipy.interpolate.make_interp_splinescipy.interpolate.interp1d