可以使用 快速构建合适的掩码KDTree
。griddata 使用的插值算法没有“有效”点的概念,因此您需要在插值之前或之后调整数据。
前:
import numpy as np
from scipy.spatial import cKDTree as KDTree
from scipy.interpolate import griddata
import matplotlib.pyplot as plt
# Some input data
t = 1.2*np.pi*np.random.rand(3000)
r = 1 + np.random.rand(t.size)
x = r*np.cos(t)
y = r*np.sin(t)
z = x**2 - y**2
# -- Way 1: seed input with nan
def excluding_mesh(x, y, nx=30, ny=30):
"""
Construct a grid of points, that are some distance away from points (x,
"""
dx = x.ptp() / nx
dy = y.ptp() / ny
xp, yp = np.mgrid[x.min()-2*dx:x.max()+2*dx:(nx+2)*1j,
y.min()-2*dy:y.max()+2*dy:(ny+2)*1j]
xp = xp.ravel()
yp = yp.ravel()
# Use KDTree to answer the question: "which point of set (x,y) is the
# nearest neighbors of those in (xp, yp)"
tree = KDTree(np.c_[x, y])
dist, j = tree.query(np.c_[xp, yp], k=1)
# Select points sufficiently far away
m = (dist > np.hypot(dx, dy))
return xp[m], yp[m]
# Prepare fake data points
xp, yp = excluding_mesh(x, y, nx=35, ny=35)
zp = np.nan + np.zeros_like(xp)
# Grid the data plus fake data points
xi, yi = np.ogrid[-3:3:350j, -3:3:350j]
zi = griddata((np.r_[x,xp], np.r_[y,yp]), np.r_[z, zp], (xi, yi),
method='linear')
plt.imshow(zi)
plt.show()
nan
这个想法是用包含值的假数据点“播种”输入数据。使用线性插值时,这些将遮盖图像中附近没有实际数据点的区域。
您还可以在插值后删除无效数据:
# -- Way 2: blot out afterward
xi, yi = np.mgrid[-3:3:350j, -3:3:350j]
zi = griddata((x, y), z, (xi, yi))
tree = KDTree(np.c_[x, y])
dist, _ = tree.query(np.c_[xi.ravel(), yi.ravel()], k=1)
dist = dist.reshape(xi.shape)
zi[dist > 0.1] = np.nan
plt.imshow(zi)
plt.show()