如何使用 numpy/scipy/matplotlib 以最小平滑绘制线(多边形链)

2024-01-11

我正在尝试在 matplotlib 中绘制一条线..我正在寻找正确的插值类型..我想要这样的东西

每条线都被平滑化。我尝试了 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)

但我得到的最好结果是

该线代表增加的变量..所以这是错误的表示。我可以搜索什么?

Thanks

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

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)

对于这种类型的图表,您想要单调的插值。这PchipInterpolator http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.PchipInterpolator.html类(您可以通过其较短的别名来引用pchip) 在 scipy.interpolate 中可以使用:

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()

Result:

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 numpy/scipy/matplotlib 以最小平滑绘制线(多边形链) 的相关文章

随机推荐