【问题标题】:Is there a way to interpolate series with recurring values using matplotlib?有没有办法使用 matplotlib 用重复值插入序列?
【发布时间】:2026-01-23 19:00:01
【问题描述】:

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

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

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 等。我的最终目标是获得平滑的曲线。

【问题讨论】:

  • 您希望插值结果是什么?例如,对于 x = 0.82,您应该得到什么值?
  • 以上两个系列的预期输出是什么?
  • 我预计插值 y 值会随着 x 的增加而逐渐增加。例如,x = 0.82 对应的 y 值将是 0.931、0.932 等。我的最终目标是获得平滑曲线。
  • @user10664643。在您的数据上下文中,您的解释毫无意义。请提供一个具体的例子

标签: python matplotlib scipy


【解决方案1】:

使用多项式怎么样?

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

【讨论】:

  • 这是拟合,不是插值。
最近更新 更多