二维插值忽略nan值

您如何告诉interp2d忽略nan值?

我的曲面x和y带有任意值z.

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]])

二维插值忽略nan值

我已经能够使用蒙版数组生成上述颜色网格,但是尝试使用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()

二维插值忽略nan值

我想简单地创建一个平滑的颜色图,其中由x和y绑定的区域根据z进行着色.

解决方法:

我不知道为什么interp2d对于不规则空间数据有问题,我建议使用griddata,您可以使用ravel将输入数据平整为向量,然后消除NaN,然后​​将其用作griddata的输入,您将得到类似这个

二维插值忽略nan值

代码与您所拥有的没有太大不同

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()

如果您必须推断数据(请在下面查看我的评论),则可以使用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))

随着kx = 1和ky = 1 f = SmoothBivariateSpline(x,y,z,kx = 1,ky = 1)你得到

二维插值忽略nan值

当kx = 2和ky = 2时,您得到:

二维插值忽略nan值

当kx = 3和ky = 3时,您得到:

二维插值忽略nan值

我更改了3张图片上的级别,因此更易于查看.但是请检查比例,在采样区域外,值可能会很快变得疯狂,因此,如果必须推断,则必须勤奋地进行

上一篇:AttributeError:“函数”对象在Python中没有属性“四元”


下一篇:python-计算点的两个阵列之间的成对角矩阵