我有一组三维坐标
编辑——
我在另一篇帖子中找到了明确的答案,使用
(x,y,z)
,我想拟合成一个空间曲线。请问是否有现有的 Python 模块可以实现此功能?根据我所找到的资料(https://docs.scipy.org/doc/scipy/reference/interpolate.html),已经存在用于将曲线拟合到一组二维坐标的模块,以及用于将曲面拟合到一组三维坐标的模块。我需要的是中间路径——将曲线拟合到一组三维坐标上。编辑——
我在另一篇帖子中找到了明确的答案,使用
interpolate.splprep()
和 interpolate.splenv()
。这是我的数据点: import numpy as np
data = np.array([[21.735556483642707, 7.9999120559310359, -0.7043281314370935],
[21.009401429607784, 8.0101161320825103, -0.16388503829177037],
[20.199370045383134, 8.0361339131845497, 0.25664085801558179],
[19.318149385194054, 8.0540100864979447, 0.50434139043379278],
[18.405497793567243, 8.0621753888918484, 0.57169888018720161],
[17.952649703401562, 8.8413995204241491, 0.39316793526155014],
[17.539007529982641, 9.6245700151356104, 0.14326173861202204],
[17.100154581079089, 10.416295524018977, 0.011339000091976647],
[16.645143439968102, 11.208477191735446, 0.070252116425261066],
[16.198247656768263, 11.967005154933993, 0.31087815045809558],
[16.661378578010989, 12.717314230004659, 0.54140549139204996],
[17.126106263351478, 13.503461982612732, 0.57743407626794219],
[17.564249250974573, 14.28890107482801, 0.42307198199366186],
[17.968265052275274, 15.031985807202176, 0.10156997950061938]])
这是我的代码:
from scipy import interpolate
from mpl_toolkits.mplot3d import Axes3D
data = data.transpose()
#now we get all the knots and info about the interpolated spline
tck, u= interpolate.splprep(data, k=5)
#here we generate the new interpolated dataset,
#increase the resolution by increasing the spacing, 500 in this example
new = interpolate.splev(np.linspace(0,1,500), tck, der=0)
#now lets plot it!
fig = plt.figure()
ax = Axes3D(fig)
ax.plot(data[0], data[1], data[2], label='originalpoints', lw =2, c='Dodgerblue')
ax.plot(new[0], new[1], new[2], label='fit', lw =2, c='red')
ax.legend()
plt.savefig('junk.png')
plt.show()
以下是图片:
可以看到,拟合效果不佳,即使我已经使用了最大允许的拟合阶数(k=5
)。这是因为曲线并非完全凸起吗?有人知道如何改进拟合吗?