简体   繁体   English

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

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

I put together a function based on this example for smoothing matplotlib lines but when I try to interpolate the y data I get the error "ValueError: x and y arrays must be equal in length along interpolation axis." 我根据示例组合了一个用于平滑matplotlib线的函数,但是当我尝试对y数据进行插值时,出现错误“ ValueError:x和y数组沿插值轴的长度必须相等”。 I assume it wants me to have an array of empty values the length of the numpy.linspace of the x data with the y data distributed correctly through it but I don't know how to do that. 我假设它希望我有一个空值数组,其长度为x数据的numpy.linspace的长度,并且y数据通过它正确分配,但是我不知道该怎么做。 I don't even know if that's right. 我什至不知道那是对的。

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)

The 1st argument to interp1d must match the second in size. interp1d的第一个参数必须与第二个参数匹配。 Together they define the original data, ie the x coordinates for the corresponding y points. 它们共同定义了原始数据,即对应的y点的x坐标。

Try: 尝试:

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

I renamed some variables to clarify what is what. 我重命名了一些变量以阐明什么是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM