Reputation: 496
I have tried a bunch of spline examples already posted for plotting smooth curves in python but those smoothed curves don't always cross through the true points. So far, I tried using
make_interp_spline
, interp1d
and also BSpline
.
Is there any way (any spline option) I can plot a smooth curve that must include/cross through true data points?
Upvotes: 2
Views: 7377
Reputation: 4537
Here is a simple example with interp1d
:
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
x = np.arange(5)
y = np.random.random(5)
fun = interp1d(x=x, y=y, kind=2)
x2 = np.linspace(start=0, stop=4, num=1000)
y2 = fun(x2)
plt.plot(x2, y2, color='b')
plt.plot(x, y, ls='', marker='o', color='r')
plt.show()
You can easily verify that this interpolation includes the true data points:
assert np.allclose(fun(x), y)
Upvotes: 4