首页 > 解决方案 > 有没有办法使用 matplotlib 插入具有重复值的系列?

问题描述

我正在尝试使用matplotlib. 我面临的问题是失败splineinterp1d因为我在 x 和 y 数组中都有重复值。

我曾尝试使用 中的样条线和interp1d函数scipy,但都因重复值问题而失败

x1 = [0.82 0.82 0.82 0.82 0.82 0.82 0.83 0.83 0.83 0.83 0.83 0.83 0.83]
y1 = [0.93 0.93 0.93 0.93 0.94 0.94 0.94 0.94 0.94 0.94 0.94 0.94 0.94]
f = interp1d(x1, y1, kind='cubic')   #this gives an error: Expect x to be a 1-D sorted array_like.



#another thing I tried

xnew = np.linspace(x1.min(),x1.max(),300)
splined = spline(x1,y1,xnew)    #this gives an error: Matrix is singular

我预计插值 y 值会随着 x 的增加而逐渐增加。因此,例如,x = 0.82 对应的 y 值将是 0.931、0.932 等。我的最终目标是获得平滑的曲线。

标签: pythonmatplotlibscipy

解决方案


使用多项式怎么样?

np.poly1d(np.polyfit(x1, y1, 2))(new_x) # 2 for second degree

推荐阅读