python - 具有 'linear' 和 'cubic' 的 Scipy griddata 产生 nan

标签 python numpy scipy interpolation

以下代码应生成网格数据。但如果我选择“三次”或“线性”插值类型,我会在 z 网格中得到 nan。我选择“最近”一切都运行良好。 下面是一个示例代码:

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

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

最佳答案

您看到的区域比您的输入点大得多。这对于“最近”并不重要,因为这总是将最近的值放置到某个坐标。但“线性”和“三次”不会外推,而是默认使用 nan 填充不在输入区域内的值。

另请参阅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 绘制时最容易理解:

enter image description here

绘图创建:

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

关于python - 具有 'linear' 和 'cubic' 的 Scipy griddata 产生 nan,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50816375/

相关文章:

python - 如何在 python 中将数据从单个 hdf5 文件安全地并行写入多个文件?

python - 如何获取多维numpy数组中最大n个值的索引

python - OSX 通过 brew 为 python3 安装 numpy

python - 规范化/标准化 numpy recarray

python - 我可以在 fortran 中使用像 numpy 和 scipy 这样的库吗?

python - 使用 lex/yacc 标记遗留数据

python - CPython的静态对象地址和碎片

python - 在 python 文档字符串中记录其他函数中可能发生的异常

python - 如何查看 scipy 的版本

python - numpy/scipy 中的指数移动和?