scipy.interpolate.splrep 函数位于数据点之上

问题描述

我正在尝试为我的数据创建一个拟合,以便我可以从拟合中随机抽取 x 点并使用 scipy.splev 计算相应的 y 点

我的代码很简单。

> fit_spline = splrep(x,y,k=1) 
> 
> plt.plot(x,'.','r')
> 
> plt.plot(fit_spline[0],fit_spline[1]) 
> 
> plt.show()

其中 x 和 y 是红点的坐标。

我使用 k=1 是因为它最适合点。然而,样条始终向上和向左移动数据点,我不知道如何解决这个问题。

This is another example. The blue line is the interpolation from splrep and the red points are the data points

The blue line is the interpolation from splrep and the red points are the data points

解决方法

您的示例代码似乎缺少 splev 步骤,如 splev(x,fit_spline)