【问题标题】:interpolate curve between three values在三个值之间插入曲线
【发布时间】:2018-07-21 15:11:16
【问题描述】:

我有以下绘制图表的脚本:

x = np.array([0,1,2])
y = np.array([5, 4.31, 4.01])
plt.plot(x, y)
plt.show()

问题是,这条线从一个点到另一个点是笔直的,但我想平滑点之间的线。

如果我使用 scipy.interpolate.spline 来平滑我的数据,我会得到以下结果:

 order = np.array([0,1,2])
 y = np.array([5, 4.31, 4.01])
 xnew = np.linspace(order.min(), order.max(), 300)
 smooth = spline(order, y, xnew)
 plt.plot(xnew, smooth)
 plt.show()

但我希望得到与给定 example 相同的结果

【问题讨论】:

  • 如果您知道潜在的相关性(例如指数衰减或其他),您可以使用 scipy.optimize.curvefit 将函数拟合到数据中。

标签: python matplotlib plot


【解决方案1】:

如果您使用的分数超过 3,您将获得与链接问题中相同的结果。 3 阶样条曲线可以通过多种方式通过 3 个点。

但您当然可以将阶数减少到 2。

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import spline

x = np.array([0,1,2])
y = np.array([5, 4.31, 4.01])
plt.plot(x, y)

xnew = np.linspace(x.min(), x.max(), 300)
smooth = spline(x, y, xnew, order=2)
plt.plot(xnew, smooth)


plt.show()

【讨论】:

    猜你喜欢
    • 2023-03-19
    • 1970-01-01
    • 2014-06-23
    • 1970-01-01
    • 2021-03-02
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 2019-10-20
    相关资源
    最近更新 更多