代码之家  ›  专栏  ›  技术社区  ›  Spencer

SciPy单调三次样条插值

  •  0
  • Spencer  · 技术社区  · 7 年前

    我想在matplotlib中画一条线。。我正在寻找正确的插值类型。。我想要这样的

    taken from canvasxpress.org/line.html

    每一条线都是平滑的。我尝试了几种scipy和matplotlib的组合,比如

    x_new = np.arange(x, x_length, 1)
    tck = interpolate.splrep(x, y, s=3)
    y_new = interpolate.splev(x_new, tck, der=0)
    ax.plot(x_new, y_new, color+lstyle)
    

    但我得到的最好结果是

    my result

    这条线代表 不断增长的变量 所以这是一个错误的表述。我可以搜索什么?

    谢谢

    编辑:我正在考虑自己实现一种方法,但我不知道是否已经实现了。。伪代码如下

    take x and y
    calculate spline for each three points 
    x[0], x[1], x[2] ... x[1], x[2], x[3] ... and so on
    for each y[n] sums every computation done for it and divide by number of 
    computations (i.e. y[1] is computed for triplette x[0..2] and x[1..3] so the 
    sum is divided by two (average for each point is taken as its value)
    
    0 回复  |  直到 13 年前
        1
  •  12
  •   Warren Weckesser    13 年前

    对于这种类型的图形,您需要 单调的 插值这个 PchipInterpolator 类(可以通过其较短的别名来引用) pchip )在scipy。可以使用插值:

    import numpy as np
    from scipy.interpolate import pchip
    import matplotlib.pyplot as plt
    
    
    # Data to be interpolated.
    x = np.arange(10.0)
    y = np.array([5.0, 10.0, 20.0, 15.0, 13.0, 22.0, 20.0, 15.0, 12.0, 16.0])
    
    # Create the interpolator.
    interp = pchip(x, y)
    
    # Dense x for the smooth curve.
    xx = np.linspace(0, 9.0, 101)
    
    # Plot it all.
    plt.plot(xx, interp(xx))
    plt.plot(x, y, 'bo')
    plt.ylim(0, 25)
    plt.grid(True)
    plt.show()
    

    结果:

    enter image description here

        2
  •  1
  •   Nicolas Barbey    13 年前

    问题不是显示问题。这是一个插值问题。使用样条函数进行插值。选择正确的插值方法在很大程度上取决于您拥有的数据类型。你不能期望插值函数在任何情况下都能正常工作(插值函数无法知道你的函数在增加)。

        3
  •  1
  •   sega_sai    13 年前

    你要么看看

    希皮。插话LSQUnivariateSpline and play with k参数(样条曲线的阶数)

    或者说scipy。插话使用k和s参数进行单变量采样和播放。

        4
  •  1
  •   Bitwise    13 年前

    重要的是要理解插值不仅仅是一条用于可视化的线。它是一个数学模型,表示您认为系统的行为(生成您测量的数据的系统)。不同类型的插值表示对系统的不同假设。

    因此,如果你知道你的系统是这样的,一个变量只能增加,你应该适合一个适当的模型(即使用适当的插值)。看看你的数据,它看起来像是一个二次多项式或一个指数函数。黄土(局部回归)拟合也会起作用。你可以使用任意一种定制功能,比如numpy。polyfit(),或使用scipy进行常规曲线拟合。优化曲线拟合()。如果你对系统有进一步的了解,你应该用它来选择适合的型号。

        5
  •  1
  •   Community Mohan Dere    9 年前

    我已经四处看看了。你想要的叫做

    单调三次插值,

    参见维基百科 here .你在mathexchange上讨论过这件事吗 here 我发现了一个python实现 here .让我知道这是否有效!