Scipy griddata with 'linear' and 'cubic' yields nan
Scipy griddata with 'linear' and 'cubic' yields nan
以下代码应生成网格数据。但是如果我选择 'cubic' 或 'linear' 作为插值类型,我会在 z 网格中得到 nan。我选择 'nearest' 一切正常 运行。
这是一个示例代码:
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()
我不知道为什么这不起作用..
你看到的区域比你输入的点大得多。这对 'nearest' 无关紧要,因为它总是将最接近的值放在某个坐标上。但是 'linear' 和 'cubic' 不外推而是默认用 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
绘制时最容易理解:
情节创建于:
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()
以下代码应生成网格数据。但是如果我选择 'cubic' 或 'linear' 作为插值类型,我会在 z 网格中得到 nan。我选择 'nearest' 一切正常 运行。 这是一个示例代码:
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()
我不知道为什么这不起作用..
你看到的区域比你输入的点大得多。这对 'nearest' 无关紧要,因为它总是将最接近的值放在某个坐标上。但是 'linear' 和 'cubic' 不外推而是默认用 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
绘制时最容易理解:
情节创建于:
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()