使用scipy.interpolate将非等间距采集的值插值成等间距的值

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import interpolate

#读数据
df=pd.read_excel("D:/Data/data_grundfox_r.xlsx" )

#绘制原始数据曲线

plt.figure(figsize=(10,5), dpi=80)
plt.subplot(121)
plt.plot(df.iloc[:,0],df.iloc[:,1],"ro")

#插值比例尺
#x = np.linspace(df.iloc[0,0],df.iloc[109,0],110)
xnew=np.linspace(df.iloc[0,0],df.iloc[109,0],220)

plt.subplot(122)
#插值方式,"nearest","zero"为阶梯插值,slinear 线性插值
for kind in ["nearest","zero","slinear"]:
    #插值函数
    f=interpolate.interp1d(df.iloc[:,0],df.iloc[:,1],kind=kind)
    #插值
    ynew=f(xnew)
    #绘制插值数据曲线
    plt.plot(xnew,ynew,label=str(kind))
plt.legend(loc="lower right")

<matplotlib.legend.Legend at 0x1276e98c2b0>

png这里写图片描述

原采样数据在x轴为非等间隔的,通过interpolate.interp1d插值后,变成了等间隔的数据,且在这种情况下使用线性插值效果较好

猜你喜欢

转载自blog.csdn.net/elite666/article/details/81750442