pythonscipycurvespline

Why does Cubic Spline create not logical shape?


I am trying to draw an arch-like Cubic Spline using SciPy's Cubic Spline function but at some point is creating a non logical shape between two of the control points. The line in black is what the function is evaluating and in green is what I expect to happen (just as it does between points 4 and 8)

This is how I create the image (You can check the code and run it here)

from scipy.interpolate import CubicSpline
import numpy as np
import matplotlib.pyplot as plt
x = [-0.0243890844, -0.0188174509, -0.00021640210000000056, 0.0202699043, 0.0239562802] # X values of the coordinates for points 2, 4, 8, 13 and 15
y = [-0.0117638968, 0.00469300617, 0.0177650191, 0.00215831073, -0.0154924048] # Y values of the coordinates for points 2, 4, 8, 13 and 15
cs = CubicSpline(x, y)
dsX = np.linspace(x[0], x[len(x)-1], num=1000)
plt.plot(dsX, cs(dsX), 'k')
plt.plot(x, y, 'mo')
plt.show()

enter image description here

Do you know how could I fix this? Or what could be causing this? Is there any kind of option/configuration parameter I am missing?


Solution

  • Cubic splines are prone to overshooting like this due to the constraint of matching 2nd derivatives. Thus small variations in data may cause large variations in the curve itself, including what you seem to have here.

    There is no way to "fix" this with CubicSpline. What you could do is to clarify your requirements and select an appropriate interpolant. If you can forgo the C2 requirement and C1 interpolant is OK, you can use pchip or Akima1D, as suggested in comments. If you want smoothing not interpolation, there's make_smoothing_spline (as also suggested in the comments).