sma*_*bro 5 python interpolation matplotlib scipy
你如何告诉 interp2d 忽略 nan 值?
我有一个带有任意值 z 的表面 x 和 y。
x = np.array([[9.19632, 9.62141, 10.0829, np.isnan, np.isnan],
[9.21164, 9.64347, 10.1392, 10.5698, np.isnan],
[9.22175, 9.65439, 10.1423, 10.6301, 11.0323],
[9.21632, 9.67060, 10.1474, 10.6230, 11.0818]])
y = np.array([[11.5466,11.6485,11.7619, np.isnan, np.isnan],
[12.4771, 12.5460, 12.5453, 12.7142, np.isnan],
[13.5578, 13.5581, 13.5505, 13.5309, 13.6081],
[14.5653, 14.5504, 14.5036, 14.5145, 14.5060]])
z = np.array([[0.466113, 0.0484404, -0.385355, np.isnan, np.isnan],
[0.366125, -0.160165, -0.548668, -0.888301,np.isnan],
[-0.0970777, -0.346734, -0.826576, -1.08412, -1.33129],
[-0.259981, -0.586938, -1.03477, -1.32384, -1.61500]])
Run Code Online (Sandbox Code Playgroud)
我已经能够使用掩码数组生成上述颜色网格,但是当我尝试使用 2d 插值创建更精细的网格时失败了。以下是我到目前为止所拥有的,请注意,我将 nan 值设置为零以获取此值,因此显然它混淆了“正确”插值。相反,我想忽略它们并将参数空间留空。
f = interp.interp2d(x,y,z, kind='linear')
xnew = np.arange(9,11.5, 0.01)
ynew = np.arange(9,15, 0.01)
znew = f(xnew, ynew)
levels = np.linspace(zmin, zmax, 15)
plt.ylabel('Y', size=15)
plt.xlabel('X', size=15)
cmap = plt.cm.jet_r
cmap.set_bad('white',0.1) # set nan to white
cs = plt.contourf(xnew, ynew, znew, levels=levels, cmap=cmap)
cbar = plt.colorbar(cs)
cbar.set_label('Z', rotation=90, fontsize=15) # gas fraction
plt.show()
Run Code Online (Sandbox Code Playgroud)
我想简单地创建一个平滑的颜色图,其中 x 和 y 边界的区域根据 z 着色。
我不知道为什么 interp2d 有不规则间隔数据的问题,我建议使用 griddata,您可以将输入数据平铺成一个向量,ravel然后消除 NaN,并将其用作 griddata 的输入,您会得到这样的东西
代码与您拥有的没有太大区别
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import griddata
x = np.array([[9.19632, 9.62141, 10.0829,np.isnan,np.isnan],
[9.21164, 9.64347, 10.1392, 10.5698,np.isnan],
[9.22175, 9.65439, 10.1423, 10.6301, 11.0323],
[9.21632, 9.67060, 10.1474, 10.6230, 11.0818]])
y = np.array([[11.5466,11.6485,11.7619,np.isnan,np.isnan],
[12.4771, 12.5460, 12.5453, 12.7142,np.isnan],
[13.5578, 13.5581, 13.5505, 13.5309, 13.6081],
[14.5653, 14.5504, 14.5036, 14.5145, 14.5060]])
z = np.array([[0.466113, 0.0484404, -0.385355,np.isnan,np.isnan],
[0.366125, -0.160165, -0.548668, -0.888301,np.isnan],
[-0.0970777, -0.346734, -0.826576, -1.08412, -1.33129],
[-0.259981, -0.586938, -1.03477, -1.32384, -1.61500]])
x=x.ravel() #Flat input into 1d vector
x=list(x[x!=np.isnan]) #eliminate any NaN
y=y.ravel()
y=list(y[y!=np.isnan])
z=z.ravel()
z=list(z[z!=np.isnan])
xnew = np.arange(9,11.5, 0.01)
ynew = np.arange(9,15, 0.01)
znew = griddata((x, y), z, (xnew[None,:], ynew[:,None]), method='linear')
levels = np.linspace(min(z), max(z), 15)
plt.ylabel('Y', size=15)
plt.xlabel('X', size=15)
cmap = plt.cm.jet_r
cs = plt.contourf(xnew, ynew, znew, levels=levels, cmap=cmap)
cbar = plt.colorbar(cs)
cbar.set_label('Z', rotation=90, fontsize=15) # gas fraction
plt.show()
Run Code Online (Sandbox Code Playgroud)
如果您必须推断数据(请查看下面我的评论),您可以使用SmoothBivariateSpline并调整样条的顺序,我不推荐这样做,我会告诉您原因。
该代码更接近您原来的代码。
from scipy.interpolate import SmoothBivariateSpline
x=x.ravel()
x=(x[x!=np.isnan])
y=y.ravel()
y=(y[y!=np.isnan])
z=z.ravel()
z=(z[z!=np.isnan])
xnew = np.arange(9,11.5, 0.01)
ynew = np.arange(10.5,15, 0.01)
f = SmoothBivariateSpline(x,y,z,kx=1,ky=1)
znew=np.transpose(f(xnew, ynew))
Run Code Online (Sandbox Code Playgroud)
随着 kx=1 和 ky=1f = SmoothBivariateSpline(x,y,z,kx=1,ky=1)你得到
随着 kx=2 和 ky=2 你得到:
并且 kx=3 和 ky=3 你得到:
我更改了 3 张图片上的级别,以便更容易查看。但是请检查比例,在您的采样区域之外,这些值可能会非常快地变得疯狂,所以如果您必须进行推断,请谨慎进行