Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

SciPy · Function

interpolate.CubicSpline

CubicSpline builds a piecewise cubic interpolant through data samples with twice-continuous derivatives. It returns a callable PPoly representation for evaluating the fitted curve.

How it works

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.

Syntax

scipy.interpolate.CubicSpline(x, y, axis=0, bc_type='not-a-knot', extrapolate=None)

Parameters

NameTypeDescription
xarray_likeOne-dimensional finite independent-variable coordinates in strictly increasing order; shape (n,).
yarray_likeFinite dependent-variable samples. Its length along axis must equal len(x), and it may have any number of dimensions.
axisint, optionalAxis of y along which the samples vary; default is 0.
bc_type2-tuple or str, optionalBoundary-condition specification for the two endpoints; default is 'not-a-knot'.
extrapolate{bool, 'periodic', None}, optionalControls out-of-bounds evaluation: a Boolean, 'periodic', or None; default is None.

Returns

A PPoly object representing the piecewise cubic interpolant.

Key points

  • The interpolant is made of cubic polynomial pieces and has continuous function values, first derivatives, and second derivatives.
  • The returned object is PPoly, with breakpoints at the supplied sample coordinates.
  • y may be multidimensional, provided its sample dimension matches x; axis selects that dimension.
  • The axis argument identifies where successive y samples correspond to successive x values.
  • Boundary conditions supply the endpoint constraints needed to determine the spline coefficients.
  • Boolean extrapolate either extends using the edge intervals or produces NaN outside the interpolation domain.

Common mistakes

  • The x coordinates cannot contain NaNs or infinities and must increase strictly; duplicate or unsorted coordinates are invalid.
  • For queries outside the input range, choose extrapolation behavior explicitly when the default is not appropriate.

Example: Evaluate a cubic spline

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.

When to use it

Smooth interpolation

Estimate values between measured samples while preserving smooth first and second derivatives.

Multidimensional sample data

Interpolate multiple signals stored along one dimension of an array.

Spline analysis

Evaluate a fitted curve and use PPoly capabilities such as derivatives or integrals.

Periodic data

Model cyclic measurements with matching endpoint behavior.

Interview questions

QWhat does CubicSpline return and what continuity does it guarantee?

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.

QWhat shape and validity requirements apply to x and y?

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.

QHow does extrapolate affect out-of-range evaluation?

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.PPoly
  • scipy.interpolate.CubicHermiteSpline
  • scipy.interpolate.make_interp_spline
  • scipy.interpolate.interp1d

Source: CubicSpline — SciPy v1.18.0 Manual. Checked 2026-10-15.