非单调数据的三次样条(不是一维函数)

Cha*_*ith 3 python interpolation

我有一条曲线,如下所示:
在此输入图像描述

该图的 x 坐标和 y 坐标为:
path_x= (4.0, 5.638304088577984, 6.785456961280076, 5.638304088577984, 4.0)
path_y =(0.0, 1.147152872702092, 2.7854569612800755, 4.423761049858059, 3.2766081771559668)

我通过以下方式获得了上面的图片:

x_min =min(path_x)-1
x_max =max(path_x)+1
y_min =min(path_y)-1
y_max =max(path_y)+1

num_pts = len(path_x)

fig = plt.figure(figsize=(8,8))
#fig = plt.figure()
plt.suptitle("Curve and the boundary")
ax = fig.add_subplot(1,1,1)

ax.set_xlim([min(x_min,y_min),max(x_max,y_max)])
ax.set_ylim([min(x_min,y_min),max(x_max,y_max)])
ax.plot(path_x,path_y)
Run Code Online (Sandbox Code Playgroud)

现在我的目的是使用三次样条绘制一条平滑的曲线。但看起来对于三次样条,您需要x coordinates按升序排列。而在本例中, 和x values都不y values按升序排列。
这也不是一个函数。也就是说,x value映射了范围内的多个元素。

我也浏览了这篇文章。但我找不到解决我的问题的正确方法。

我非常感谢你在这方面的帮助

Gir*_*rdi 6

正如评论中所建议的,您始终可以使用任意(和线性!)参数对任何曲线/曲面进行参数化。

例如,定义t为参数,以便您得到x=x(t)y=y(t)。由于t是任意的,您可以定义它,以便在 处t=0获得第一个坐标path_x[0],path_y[0],在 处t=1获得最后一对坐标path_x[-1],path_y[-1]

这是一个使用的代码scipy.interpolate

import numpy
import scipy.interpolate
import matplotlib.pyplot as plt

path_x = numpy.asarray((4.0, 5.638304088577984, 6.785456961280076, 5.638304088577984, 4.0),dtype=float)
path_y = numpy.asarray((0.0, 1.147152872702092, 2.7854569612800755, 4.423761049858059, 3.2766081771559668),dtype=float)

# defining arbitrary parameter to parameterize the curve
path_t = numpy.linspace(0,1,path_x.size)

# this is the position vector with
# x coord (1st row) given by path_x, and
# y coord (2nd row) given by path_y
r = numpy.vstack((path_x.reshape((1,path_x.size)),path_y.reshape((1,path_y.size))))

# creating the spline object
spline = scipy.interpolate.interp1d(path_t,r,kind='cubic')

# defining values of the arbitrary parameter over which
# you want to interpolate x and y
# it MUST be within 0 and 1, since you defined
# the spline between path_t=0 and path_t=1
t = numpy.linspace(numpy.min(path_t),numpy.max(path_t),100)

# interpolating along t
# r[0,:] -> interpolated x coordinates
# r[1,:] -> interpolated y coordinates
r = spline(t)

plt.plot(path_x,path_y,'or')
plt.plot(r[0,:],r[1,:],'-k')
plt.xlabel('x')
plt.ylabel('y')
plt.show()
Run Code Online (Sandbox Code Playgroud)

带输出

二维三次样条