
- SciPy - Home
- SciPy - Introduction
- SciPy - Environment Setup
- SciPy - Basic Functionality
- SciPy - Relationship with NumPy
- SciPy Clusters
- SciPy - Clusters
- SciPy - Hierarchical Clustering
- SciPy - K-means Clustering
- SciPy - Distance Metrics
- SciPy Constants
- SciPy - Constants
- SciPy - Mathematical Constants
- SciPy - Physical Constants
- SciPy - Unit Conversion
- SciPy - Astronomical Constants
- SciPy - Fourier Transforms
- SciPy - FFTpack
- SciPy - Discrete Fourier Transform (DFT)
- SciPy - Fast Fourier Transform (FFT)
- SciPy Integration Equations
- SciPy - Integrate Module
- SciPy - Single Integration
- SciPy - Double Integration
- SciPy - Triple Integration
- SciPy - Multiple Integration
- SciPy Differential Equations
- SciPy - Differential Equations
- SciPy - Integration of Stochastic Differential Equations
- SciPy - Integration of Ordinary Differential Equations
- SciPy - Discontinuous Functions
- SciPy - Oscillatory Functions
- SciPy - Partial Differential Equations
- SciPy Interpolation
- SciPy - Interpolate
- SciPy - Linear 1-D Interpolation
- SciPy - Polynomial 1-D Interpolation
- SciPy - Spline 1-D Interpolation
- SciPy - Grid Data Multi-Dimensional Interpolation
- SciPy - RBF Multi-Dimensional Interpolation
- SciPy - Polynomial & Spline Interpolation
- SciPy Curve Fitting
- SciPy - Curve Fitting
- SciPy - Linear Curve Fitting
- SciPy - Non-Linear Curve Fitting
- SciPy - Input & Output
- SciPy - Input & Output
- SciPy - Reading & Writing Files
- SciPy - Working with Different File Formats
- SciPy - Efficient Data Storage with HDF5
- SciPy - Data Serialization
- SciPy Linear Algebra
- SciPy - Linalg
- SciPy - Matrix Creation & Basic Operations
- SciPy - Matrix LU Decomposition
- SciPy - Matrix QU Decomposition
- SciPy - Singular Value Decomposition
- SciPy - Cholesky Decomposition
- SciPy - Solving Linear Systems
- SciPy - Eigenvalues & Eigenvectors
- SciPy Image Processing
- SciPy - Ndimage
- SciPy - Reading & Writing Images
- SciPy - Image Transformation
- SciPy - Filtering & Edge Detection
- SciPy - Top Hat Filters
- SciPy - Morphological Filters
- SciPy - Low Pass Filters
- SciPy - High Pass Filters
- SciPy - Bilateral Filter
- SciPy - Median Filter
- SciPy - Non - Linear Filters in Image Processing
- SciPy - High Boost Filter
- SciPy - Laplacian Filter
- SciPy - Morphological Operations
- SciPy - Image Segmentation
- SciPy - Thresholding in Image Segmentation
- SciPy - Region-Based Segmentation
- SciPy - Connected Component Labeling
- SciPy Optimize
- SciPy - Optimize
- SciPy - Special Matrices & Functions
- SciPy - Unconstrained Optimization
- SciPy - Constrained Optimization
- SciPy - Matrix Norms
- SciPy - Sparse Matrix
- SciPy - Frobenius Norm
- SciPy - Spectral Norm
- SciPy Condition Numbers
- SciPy - Condition Numbers
- SciPy - Linear Least Squares
- SciPy - Non-Linear Least Squares
- SciPy - Finding Roots of Scalar Functions
- SciPy - Finding Roots of Multivariate Functions
- SciPy - Signal Processing
- SciPy - Signal Filtering & Smoothing
- SciPy - Short-Time Fourier Transform
- SciPy - Wavelet Transform
- SciPy - Continuous Wavelet Transform
- SciPy - Discrete Wavelet Transform
- SciPy - Wavelet Packet Transform
- SciPy - Multi-Resolution Analysis
- SciPy - Stationary Wavelet Transform
- SciPy - Statistical Functions
- SciPy - Stats
- SciPy - Descriptive Statistics
- SciPy - Continuous Probability Distributions
- SciPy - Discrete Probability Distributions
- SciPy - Statistical Tests & Inference
- SciPy - Generating Random Samples
- SciPy - Kaplan-Meier Estimator Survival Analysis
- SciPy - Cox Proportional Hazards Model Survival Analysis
- SciPy Spatial Data
- SciPy - Spatial
- SciPy - Special Functions
- SciPy - Special Package
- SciPy Advanced Topics
- SciPy - CSGraph
- SciPy - ODR
- SciPy Useful Resources
- SciPy - Reference
- SciPy - Quick Guide
- SciPy - Cheatsheet
- SciPy - Useful Resources
- SciPy - Discussion
SciPy - interpolate.CubicSpline() Function
scipy.interpolate.CubicSpline() is a function in SciPy that performs cubic spline interpolation. This method for constructing smooth curves through a set of points. When the given arrays of x and y coordinates then CubicSpline() creates a piecewise cubic polynomial that passes through each data point with continuous first and second derivatives.
The users can control boundary conditions such as natural i.e., second derivative is zero at endpoints or clamped i.e., specifying first derivatives at endpoints. The result is an interpolated function that can estimate values at intermediate points with high accuracy and smoothness by making it ideal for smooth data approximation.
Syntax
Following is the syntax of the function scipy.interpolate.CubicSpline() to perform cubic spline interpolation −
CubicSpline(x, y, axis=0, bc_type='not-a-knot', extrapolate=None)
Parameters
Below are the parameters of the scipy.interpolate.CubicSpline() function −
- x(array-like, shape(n,)): The x-coordinates of the data points. They must be in strictly increasing order.
- y(array-like, shape(n,)):The y-coordinates of the data points. This can be multidimensional. The interpolation is performed along the specified axis.
- axis(int, optional): This specifies the axis in the y array that corresponds to the x coordinates. This allows for multi-dimensional y arrays where interpolation is only along the specified axis. The default value is 0.
- bc_type(string or 2-tuple, optional): Defines the boundary conditions such as not-a-knot, clamped, natural
- extrapolate(bool or 'periodic', optional): This parameter specifies whether to extrapolate the spline outside of the data range.
Return Value
The scipy.interpolate.CubicSpline() function returns an object that represents a cubic spline interpolation of the input data (x, y).
Basic Interpolation
Following is the example of scipy.interpolate.CubicSpline() function for performing the cubic spline interpolation. This example shows how to create a smooth curve that connects each point smoothly which is ideal for data fitting and analysis −
import numpy as np import matplotlib.pyplot as plt from scipy.interpolate import CubicSpline # Sample data points x = np.array([0, 1, 2, 3, 4, 5]) y = np.array([1, 0, 1, 0, 1, 0]) # Create cubic spline interpolation cs = CubicSpline(x, y) # Generate new x values for a smooth curve x_new = np.linspace(0, 5, 100) y_new = cs(x_new) # Plot the original data points and the cubic spline plt.plot(x, y, 'o', label='Data Points') plt.plot(x_new, y_new, label='Cubic Spline Interpolation') plt.xlabel('x') plt.ylabel('y') plt.legend() plt.title('Basic Cubic Spline Interpolation') plt.show()
Here is the output of the scipy.interpolate.CubicSpline() function basic example −

Clamped Boundary Condition
In a clamped boundary condition the spline's first derivative i.e., slope is specified at both endpoints. This can be useful when we know the slope at the start and end points of the data. Heres how to apply the clamped condition in a CubicSpline interpolation −
import numpy as np import matplotlib.pyplot as plt from scipy.interpolate import CubicSpline # Sample data points x = np.array([0, 1, 2, 3, 4, 5]) y = np.array([0, 1, 0, 1, 0, 1]) # Specify the slopes (first derivatives) at the endpoints # Here, let's assume we want a slope of 0 at both ends bc_type = ((1, 0.0), (1, 0.0)) # (1, slope) specifies the derivative order and value # Create cubic spline with clamped boundary condition cs = CubicSpline(x, y, bc_type=bc_type) # Generate new x values for smooth curve x_new = np.linspace(0, 5, 100) y_new = cs(x_new) # Plot the original data points and the cubic spline with clamped condition plt.plot(x, y, 'o', label='Data Points') plt.plot(x_new, y_new, label='Clamped Cubic Spline') plt.xlabel('x') plt.ylabel('y') plt.legend() plt.title('Cubic Spline with Clamped Boundary Condition') plt.show()
Here is the output of the scipy.interpolate.CubicSpline() function performed clamped boundary −

Calculating the Derivative
In this example we calculate and visualize the first derivative of the cubic spline −
import numpy as np import matplotlib.pyplot as plt from scipy.interpolate import CubicSpline # Sample data points x = np.array([0, 1, 2, 3, 4, 5]) y = np.array([0, 1, 0, 1, 0, 1]) # Specify the slopes (first derivatives) at the endpoints # Here, let's assume we want a slope of 0 at both ends bc_type = ((1, 0.0), (1, 0.0)) # (1, slope) specifies the derivative order and value # Create cubic spline with clamped boundary condition cs = CubicSpline(x, y, bc_type=bc_type) # Generate new x values for smooth curve x_new = np.linspace(0, 5, 100) y_new = cs(x_new) # Plot the original data points and the cubic spline with clamped condition plt.plot(x, y, 'o', label='Data Points') plt.plot(x_new, y_new, label='Clamped Cubic Spline') plt.xlabel('x') plt.ylabel('y') plt.legend() plt.title('Cubic Spline with Clamped Boundary Condition') plt.show()
Here is the output of the scipy.interpolate.CubicSpline() function used to performed Derivative−
