如何克服 scipy.interpolate.interp1d() 中的 y 数组长度错误?

how can I overcome y array length error in scipy.interpolate.interp1d()?

我根据 this 示例整理了一个函数来平滑 matplotlib 线,但是当我尝试插入 y 数据时出现错误 "ValueError: x and y arrays must be equal in length along interpolation axis." 我假设它希望我有一个数组空值是 x 数据的 numpy.linspace 的长度,y 数据通过它正确分布,但我不知道该怎么做。我什至不知道这对不对。

def spline_it(key):
        y = [i[1] for i in datapoints[key]]
        x_smooth = np.linspace(0,len(y),len(y)*10)
        y_smooth = interp1d(x_smooth, y, kind='cubic')
        return x_smooth, y_smooth(x_smooth)

interp1d 的第一个参数必须与第二个参数大小匹配。它们一起定义了原始数据,即对应 y 点的 x 坐标。

尝试:

def spline_it(key):
    y = [i[1] for i in datapoints[key]]
    x = np.arange(len(y))
    interpolator = interp1d(x, y, kind='cubic')
    x_smooth = np.linspace(0,len(y),len(y)*10)
    y_smooth = interpolator(x_smooth)
    return x_smooth, y_smooth

我重命名了一些变量以阐明什么是什么。