【问题标题】:Curve fitting using python使用python进行曲线拟合
【发布时间】:2021-11-14 23:04:15
【问题描述】:

我正在尝试使用 numpy 和 scipy 库为一组点拟合曲线,但得到如下所示的闭合曲线。
谁能告诉我如何在不闭合曲线的情况下拟合曲线?

我遵循的代码是:

import numpy as np
from scipy.interpolate import splprep, splev
import matplotlib.pyplot as plt

coords = np.array([(3,8),(3,9),(4,10),(5,11),(6,11), (7,13), (9,13),(10,14),(11,14),(12,14),(14,16),(16,17),(17,18),(18,18),(19,18), (20,19),
        (21,19),(22,20),(23,20),(24,21),(26,21),(27,21),(28,21),(30,21),(32,20),(33,20),(32,17),(33,16),(33,15),(34,12), (34,10),(33,10),
         (33,9),(33,8),(33,6),(34,6),(34,5)])

tck, u = splprep(coords.T, u=None, s=0.0, per=1) 
u_new = np.linspace(u.min(), u.max(), 1000)
x_new, y_new = splev(u_new, tck, der=0)

plt.plot(coords[:,1], coords[:,0], 'ro')
plt.plot(y_new, x_new, 'b--')
plt.show()

输出:

我需要输出而不加入第一个和最后一个点。

谢谢。

【问题讨论】:

    标签: python numpy matplotlib scipy data-visualization


    【解决方案1】:

    只需将scipy.interpolate.splprep中的per参数设置为0即可:

    tck, u = splprep(coords.T, u=None, s=0.0, per=0)
    

    【讨论】:

    • 谢谢@Zephyr
    猜你喜欢
    • 2015-07-04
    • 1970-01-01
    • 2021-10-01
    • 1970-01-01
    • 2015-09-14
    • 2020-03-19
    • 2018-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多