I'm writing a python script to interpolate a given set of points with splines. The points are defined by their [x, y]
coordinates.
I tried to use this code:
x = np.array([23, 24, 24, 25, 25])
y = np.array([13, 12, 13, 12, 13])
tck, u = scipy.interpolate.splprep([x,y], s=0)
unew = np.arange(0, 1.00, 0.005)
out = scipy.interpolate.splev(unew, tck)
which gives me a curve like this:
However, I need to have a smooth closed curve - on the picture above the derivatives at one of the points are obviously not the same. How can I achieve this?
Your closed path can be considered as a parametric curve, x=f(u), y=g(u) where u is distance along the curve, bounded on the interval [0, 1). You can use
scipy.interpolate.splprep
withper=True
to treat yourx
andy
points as periodic, then evaluate the fitted splines usingscipy.interpolate.splev
: