插值类所在模块:
scipy.interpolate
interpolate.interp1d(x, y, kind = 'linear' ,...)
kind 的可选参数为:
'nearest' ,'zero':阶梯插值,0阶样条插值 'slinear' ,'linear' : 线性插值 'quadratic','cubic': 二阶,三阶样条插值
interpolate.interp2d(x, y, kind = 'linear' ,...)
使用
s
i
n
sin
sin函数的
10
10
10个点作为原始数据,采用
P
y
t
h
o
n
Python
Python中的不同方法插值.
代码:
import numpy as np import matplotlib.pyplot as plt from scipy.interpolate import interp1d kind = ['nearest','zero',#阶梯插值,0阶样条插值 'slinear' ,'linear', #: 线性插值 'quadratic','cubic']#: 二阶,三阶样条插值 x = np.arange(11) y = np.sin(x) u = np.arange(0,10.05,0.05) #需要插值的数据点 fig,axes = plt.subplots(2,3) ax =axes.ravel() #将二维数组转化为一维 for i,kd in enumerate(kind): ax[i].plot(x,y,'o',mec='k',mfc='w',ms=15) f = interp1d(x, y,kind=kd) v = f(u) ax[i].plot(u,v,'b',label=kd) ax[i].legend()
结果: