Finding derivative of a spline in Python using SciPy

In this tutorial, we will learn how to find derivative of a spline in Python using SciPy.

Here we have used:


First of all, we have to be familiar with the word spline. The spline is a piecewise polynomial function and this function is used in interpolating problems, specifically spline interpolation is mostly preferred as a method of estimating values between known data points.

The derivative of  a spline – SciPy

here, we are focusing on the cubic spline. we can easily get cubic spline of any data by using the following library

from scipy.interpolate import CubicSpline


here, for the x-axis, we are considering an array of nine elements

and for the y-axis, we are considering the array of sine values of nine elements.

from scipy.interpolate import CubicSpline
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(10)
y = np.sin(x)
cs = CubicSpline(x, y)
s = np.arange(-1, 10, 0.1)
fig, p = plt.subplots(figsize=(8, 4))
p.plot(x, y, 'o', label='value')
p.plot(s, np.sin(s), label='original')
p.plot(s, cs(s), label="C")
p.plot(s, cs(s, 1), label="C1")
p.plot(s, cs(s, 2), label="linear")
p.set_xlim(-0.5, 14)
p.legend(loc='upper right', ncol=3)

Output :

how to find derivative of a spline in Python using SciPy

Changes in values can be observed in the graph.

You may also read:

Leave a Reply

Your email address will not be published. Required fields are marked *