使用插值在 4 个点之间绘制曲线

Curve between 4 points using interpolation

我有 4 个已知点,我正在尝试 运行 一条平滑的曲线。

gg_xy=np.array([[-2.612,0],[0,1.6969999999999996],[0.5870000000000001,0],[0,-2.605]])
plt.plot(gg_xy[:,0],gg_xy[:,1],'ro')

ggx,ggy=splprep(gg_xy.T,u=None,s=0.0,per=1)
gg_xspline=np.linspace(ggy.min(),ggy.max(),300)
ggxnew,ggynew=splev(gg_xspline,ggx,der=0)
plt.plot(ggxnew,ggynew)
plt.show()

这是我的输出:

插值时漏掉了一个点。有人可以帮我强迫它通过这一点吗?除了使用样条插值之外,还有更好的方法吗?编辑:曲线必须是单个连接环。 谢谢!

from scipy.interpolate import splprep, splev
import matplotlib.pyplot as plt
import numpy as np

gg_xy=np.array([[-2.612,0],[0,1.6969999999999996],
                [0.5870000000000001,0],[0,-2.605], [0,-2.605]])
plt.plot(gg_xy[:,0],gg_xy[:,1],'ro')

ggx,ggy=splprep(gg_xy.T,u=None,s=0.0,per=1)
gg_xspline=np.linspace(ggy.min(),ggy.max(),300)
ggxnew,ggynew=splev(gg_xspline,ggx,der=0)
plt.plot(ggxnew,ggynew)
plt.show()

文档:

per: int, optional

If non-zero, data points are considered periodic with period x[m-1] - x[0] and a smooth periodic spline approximation is returned. Values of y[m-1] and w[m-1] are not used.

看来它忽略了最后一点,所以我只是重复了最后一点。