如何用numpy/scipy/matplotlib绘制线条(多边形链),最小化平滑度

gc5*_*gc5 8 python numpy matplotlib spline scipy

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

取自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)
Run Code Online (Sandbox Code Playgroud)

但我得到的最好结果是

我的结果

该线表示增加的变量 ..因此它是错误的表示.我可以搜索什么?

谢谢

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

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)
Run Code Online (Sandbox Code Playgroud)

War*_*ser 10

对于那种类型的图形,您需要单调插值.可以使用scipy.interpolate中的PchipInterpolator类(可以通过其较短的别名引用pchip):

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()
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述