这是一个基于您的代码比较线性、二次和三次插值的示例。只有线性插值不在右侧数据点之上。除了线性插值之外,我所知道的唯一通用方法是裁剪——我个人认为这是一种不好的做法。
from scipy import interpolate
from numpy import linspace
import matplotlib
import matplotlib.pyplot as plt
y = [5, 0, 4, 4]
x = linspace(0, len(y)-1, num=len(y), endpoint=True)
f1 = interpolate.interp1d(x, y, kind='linear')
f2 = interpolate.interp1d(x, y, kind='quadratic')
f3 = interpolate.interp1d(x, y, kind='cubic')
xnew = linspace(0, len(y)-1, num=100, endpoint=True)
plt.title('Interpolation comparison')
plt.plot(xnew, f1(xnew), linestyle='solid', color='red', label='linear');
plt.plot(xnew, f2(xnew), linestyle='dashed', color='green', label='quadratic');
plt.plot(xnew, f3(xnew), linestyle='dashdot', color='blue', label='cubic');
plt.scatter(x=[i for i in range(len(x))],y=y, color='black');
plt.legend() # turn on the legend
plt.show()