bundles / scipy 2.0.0.dev0+git20260907.41eeb59 / scipy / interpolate / _cubic / CubicSpline
class
scipy.interpolate._cubic:CubicSpline
source: build-install/usr/lib/python3.14/site-packages/scipy/interpolate/_cubic.py:638
Signature
class CubicSpline(x, y, axis = 0, bc_type = not-a-knot, extrapolate = None)Members
Summary
Piecewise cubic interpolator to fit values (C2 smooth).
Extended Summary
Interpolate data with a piecewise cubic polynomial which is twice continuously differentiable [1]. The result is represented as a PPoly instance with breakpoints matching the given data.
Parameters
x: array_like, shape (n,)1-D array containing values of the independent variable. Values must be real, finite and in strictly increasing order.
y: array_likeArray containing values of the dependent variable. It can have arbitrary number of dimensions, but the length along
axis(see below) must match the length ofx. Values must be finite.axis: int, optionalAxis along which
yis assumed to be varying. Meaning that forx[i]the corresponding values arenp.take(y, i, axis=axis). Default is 0.bc_type: str or 2-tuple, optionalBoundary condition type. Two additional equations, given by the boundary conditions, are required to determine all coefficients of polynomials on each segment [2].
If
bc_typeis a string, then the specified condition will be applied at both ends of a spline. Available conditions are:'not-a-knot' (default): The first and second segment at a curve end are the same polynomial. It is a good default when there is no information on boundary conditions.
'periodic': The interpolated functions is assumed to be periodic of period
x[-1] - x[0]. The first and last value ofymust be identical:y[0] == y[-1]. This boundary condition will result iny'[0] == y'[-1]andy''[0] == y''[-1].'clamped': The first derivative at curves ends are zero. Assuming a 1D
y,bc_type=((1, 0.0), (1, 0.0))is the same condition.'natural': The second derivative at curve ends are zero. Assuming a 1D
y,bc_type=((2, 0.0), (2, 0.0))is the same condition.
If
bc_typeis a 2-tuple, the first and the second value will be applied at the curve start and end respectively. The tuple values can be one of the previously mentioned strings (except 'periodic') or a tuple(order, deriv_values)allowing to specify arbitrary derivatives at curve ends:order: the derivative order, 1 or 2.deriv_value: array_like containing derivative values, shape must be the same asy, excludingaxisdimension. For example, ifyis 1-D, thenderiv_valuemust be a scalar. Ifyis 3-D with the shape (n0, n1, n2) and axis=2, thenderiv_valuemust be 2-D and have the shape (n0, n1).
extrapolate: {bool, 'periodic', None}, optionalIf bool, determines whether to extrapolate to out-of-bounds points based on first and last intervals, or to return NaNs. If 'periodic', periodic extrapolation is used. If None (default),
extrapolateis set to 'periodic' forbc_type='periodic'and to True otherwise. See tutorial-interpolate_out_of_bounds.
Attributes
x: ndarray, shape (n,)Breakpoints. The same
xwhich was passed to the constructor.c: ndarray, shape (4, n-1, ...)Coefficients of the polynomials on each segment. The trailing dimensions match the dimensions of
y, excludingaxis. For example, ifyis 1-d, thenc[k, i]is a coefficient for(x-x[i])**(3-k)on the segment betweenx[i]andx[i+1].axis: intInterpolation axis. The same axis which was passed to the constructor.
Methods
__call__derivativeantiderivativeintegratesolveroots
Notes
Parameters bc_type and extrapolate work independently, i.e. the former controls only construction of a spline, and the latter only evaluation.
When a boundary condition is 'not-a-knot' and n = 2, it is replaced by a condition that the first derivative is equal to the linear interpolant slope. When both boundary conditions are 'not-a-knot' and n = 3, the solution is sought as a parabola passing through given points.
When 'not-a-knot' boundary conditions is applied to both ends, the resulting spline will be the same as returned by splrep (with s=0) and InterpolatedUnivariateSpline, but these two methods use a representation in B-spline basis.
Examples
In this example the cubic spline is used to interpolate a sampled sinusoid. You can see that the spline continuity property holds for the first and second derivatives and violates only for the third derivative.import numpy as np from scipy.interpolate import CubicSpline import matplotlib.pyplot as plt x = np.arange(10) y = np.sin(x) cs = CubicSpline(x, y) xs = np.arange(-0.5, 9.6, 0.1) fig, ax = plt.subplots(figsize=(6.5, 4))✓
ax.plot(x, y, 'o', label='data') ax.plot(xs, np.sin(xs), label='true') ax.plot(xs, cs(xs), label="S") ax.plot(xs, cs(xs, 1), label="S'") ax.plot(xs, cs(xs, 2), label="S''") ax.plot(xs, cs(xs, 3), label="S'''") ax.set_xlim(-0.5, 9.5) ax.legend(loc='lower left', ncol=2)✗
<matplotlib.legend.Legend object at 0x7f3f60f04ad0>
plt.show()
✓
theta = 2 * np.pi * np.linspace(0, 1, 5) y = np.c_[np.cos(theta), np.sin(theta)] cs = CubicSpline(theta, y, bc_type='periodic') print("ds/dx={:.1f} ds/dy={:.1f}".format(cs(0, 1)[0], cs(0, 1)[1])) xs = 2 * np.pi * np.linspace(0, 1, 100) fig, ax = plt.subplots(figsize=(6.5, 4))✓
ax.plot(y[:, 0], y[:, 1], 'o', label='data') ax.plot(np.cos(xs), np.sin(xs), label='true') ax.plot(cs(xs)[:, 0], cs(xs)[:, 1], label='spline')✗
[<matplotlib.lines.Line2D object at 0x7f3f68867620>]
ax.axes.set_aspect('equal')
✓ax.legend(loc='center')
✗<matplotlib.legend.Legend object at 0x7f3f68867a10>
plt.show()
✓
cs = CubicSpline([0, 1], [0, 1], bc_type=((1, 0), (1, 3))) x = np.linspace(0, 1) np.allclose(x**3, cs(x))✓
True
See also
- Akima1DInterpolator
Akima 1D interpolator.
- PPoly
Piecewise polynomial in terms of coefficients and breakpoints.
- PchipInterpolator
PCHIP 1-D monotonic cubic interpolator.
Aliases
scipy.interpolate.CubicSpline