具有“线性"和“三次"的Scipy网格数据会产生nan [英] Scipy griddata with 'linear' and 'cubic' yields nan

查看:126
本文介绍了具有“线性"和“三次"的Scipy网格数据会产生nan的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码应产生griddata.但是,如果我选择三次"或线性"作为插值类型,我会在z网格中得到nan.我选择的是最近的",一切运行正常. 这是示例代码:

the following code should produce griddata. But in case I choose as interpolation type 'cubic' or 'linear' I am getting nan's in the z grid. Wen i'm choosing 'nearest' everything is running fine. Here is an example code:

import numpy as np
from scipy.interpolate import griddata

x = np.array([0.03,0.05,0033])
y = np.array([0.004,0.01,0.02])
z = np.array([1,2,3])


xy = np.zeros((2,np.size(x)))
xy[0] = x
xy[1] = y
xy = xy.T

grid_x, grid_y = np.mgrid[0.0:0.09:250*1j, 0.0:0.03:250*1j] #generating the grid


i_type= 'cubic' #nearest, linear, cubic
grid_z = griddata(xy, z, (grid_x, grid_y), method=i_type)

#check if there is a nan in the z grid:
print np.isnan(grid_z).any()

我不知道为什么这不起作用.

I don't have any idea why this is not working..

推荐答案

您所看到的区域比输入点大得多.对于最近"而言,这无关紧要,因为这总是将最接近的值放在某个坐标上.但是'linear'和'cubic'不会外推,而是默认使用nan来填充不在输入区域内的值.

Your area you look at is simply much larger than your input points. This doesn't matter for 'nearest' as this always puts the nearest value to a certain coordinate. But 'linear' and 'cubic' do not extrapolate but fill the values which are not within the input area with nan by default.

另请参阅griddata的文档:

fill_value : float, optional
Value used to fill in for requested points outside of the convex hull of the input points. If not provided, then the default is nan. This option has no effect for the ‘nearest’ method.

imshow绘制时最容易理解:

使用以下方式创建的图

import numpy as np
from scipy.interpolate import griddata

x = np.array([0.03,0.05,0.033])
y = np.array([0.004,0.01,0.02])
z = np.array([1,2,3])


xy = np.zeros((2,np.size(x)))
xy[0] = x
xy[1] = y
xy = xy.T

grid_x, grid_y = np.mgrid[0.0:0.09:250*1j, 0.0:0.03:250*1j] #generating the grid

fig, axs = plt.subplots(3)
for i, i_type in enumerate(['cubic', 'nearest', 'linear']): #, cubic
    grid_z = griddata(xy, z, (grid_x, grid_y), method=i_type)

    #check if there is a nan in the z grid:
    axs[i].imshow(grid_z)
    axs[i].set_title(i_type)

plt.tight_layout()

这篇关于具有“线性"和“三次"的Scipy网格数据会产生nan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆