插值
# 通俗理解:
在一段连续的函数中,插入指定n个点, 填满空隙
一维插值
import matplotlib.pyplot as plt
import numpy as np
from scipy import interpolate
import pylab as pl
# 生成 11 一个点
x = np.linspace(0,10,11) # x 轴
y = np.sin(x) # y轴
# 在 0 - 10的范围内 插入 101 个点
x_new = np.linspace(0,10,101)
pl.plot(x,y,'ro')
for kind in ['nearest','zero','slinear','quadratic','cubic']: # 插值方式
# nearest 和 zero 为 阶梯插值
# slinear 线性插值
# quadratic 和 cubic 为2阶,3阶 B样条曲线插值
f = interpolate.interp1d(x,y,kind=kind)
# “slinear”、“quadratic”和“cubic”指一阶、二阶或三阶样条插值)
y_new = f(x_new)
pl.plot(x_new,y_new,label=str(kind))
pl.legend(loc='lower right')
plt.savefig(u'./一维插值.png')
![一维插值](https://images.cnblogs.com/cnblogs_com/dengz/1973384/t_210511025549%E4%B8%80%E7%BB%B4%E6%8F%92%E5%80%BC.png)
二维插值
# -*- coding:utf-8 -*-
import numpy as np
from scipy import interpolate
import pylab as pl
import matplotlib as mpl
def func(x,y):
'''
x 的2次幂
y 的2次幂
'''
return (x+y)*np.exp(-5.0*(x**2+y**2))
# X Y 轴 分为 15*15 的网格
y,x = np.mgrid[-1:1:15j,-1:1:15j]
fvals = func(x,y) # 计算每个网格上的函数值
print len(fvals[0])
# 三次 样条 二维插值
new_func = interpolate.interp2d(x,y,fvals,kind='cubic')
# 计算 100 * 100 网格上插值
x_new = np.linspace(-1,1,100)
y_new = np.linspace(-1,1,100)
f_new = new_func(x_new,y_new) # 仅是y值
# 绘图
# 为了更明显的比较插值前后的区别,使用关键字参数interpolation='nearest'
# 关闭 imshow()内置的插值运算
pl.subplot(121)
# pl.cm.jet 和 pl.cm.hot
im1 = pl.imshow(fvals,extent=[-1,1,-1,1],cmap=mpl.cm.hot,interpolation='nearest',origin='lower')
pl.colorbar(im1)
pl.subplot(122)
im2 = pl.imshow(f_new,extent=[-1,1,-1,1],cmap=mpl.cm.hot,interpolation='nearest',origin='lower')
pl.colorbar(im2)
pl.savefig(u'./二维插值')
pl.show()
# 左图为原始数据,右图为二维插值结果图。
![二维插值](https://images.cnblogs.com/cnblogs_com/dengz/1973384/t_210511025605%E4%BA%8C%E7%BB%B4%E6%8F%92%E5%80%BC.png?a=1620701814752)
二维插值的三维展示方法
# -*- coding: utf-8 -*-
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib as mpl
from scipy import interpolate
import matplotlib.cm as cm
import matplotlib.pyplot as plt
def func(x, y):
# exp() 方法返回x的指数,ex。
return (x + y) * np.exp(-5 * (x ** 2 + y ** 2))
x = np.linspace(-1, 1, 20) # linspace 是创建等差数列的函数 一维矩阵,
y = np.linspace(-1, 1, 20)
x, y = np.meshgrid(x, y) # 转换成坐标的形式 20*20的网格数据
fvals = func(x, y) # 计算每个网格点上的函数值
fig = plt.figure(figsize=(9, 6))
# 绘图
ax = plt.subplot(1, 2, 1, projection='3d')
surf = ax.plot_surface(x, y, fvals, rstride=2, cstride=2, cmap=cm.coolwarm, linewidth=0.5, antialiased=True)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('f(x,y)')
plt.colorbar(surf, shrink=0.5, aspect=5) # 标注
# 二维插值
# 函数
new_func = interpolate.interp2d(x, y, fvals, kind='cubic')
# 计算100 * 100 的网格上的插值
x_new = np.linspace(-1, 1, 100)
y_new = np.linspace(-1, 1, 100)
f_new = new_func(x_new, y_new)
x_new, y_new = np.meshgrid(x_new, y_new)
ax2 = plt.subplot(1, 2, 2, projection='3d')
surf2 = ax2.plot_surface(x_new, y_new, f_new, rstride=2, cstride=2, cmap=cm.coolwarm, linewidth=0.5, antialiased=True)
ax2.set_xlabel('x_new')
ax2.set_xlabel('y_new')
ax2.set_zlabel('f_new(x,y)')
plt.colorbar(surf2, shrink=0.5, aspect=5)
plt.show()
### 左图的二维数据集的函数值由于样本较少,会显得粗糙。而右图对二维样本数据进行三次样条插值,拟合得到更多数据点的样本值,绘图后图像明显光滑多了。
![二维插值的三维展示方法](https://images.cnblogs.com/cnblogs_com/dengz/1973384/t_210511025616%E4%BA%8C%E7%BB%B4%E6%8F%92%E5%80%BC%E7%9A%84%E4%B8%89%E7%BB%B4%E5%B1%95%E7%A4%BA%E6%96%B9%E6%B3%95.png?a=1620701814752)