我试图基于地球物理学以x,y,z点的形式对表面进行插值。我希望将表面裁切到调查范围内(例如,我只想要下图红色边框内的区域)。
有人知道该怎么做吗?下面是一些生成下图的示例代码。我需要弄清楚如何对其进行修改以仅对红色边框内的区域进行插值。
# Import libs
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import griddata
# Create some example data
x = np.array([1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6])
y = np.array([1,2,3,4,2,3,4,5,3,4,5,6,4,5,6,7,5,6,7,8,6,7,8,9])
z = np.linspace(0,100,len(y))
# Grid and interpolate between points
yi, xi = np.mgrid[int(y.min()):int(y.max()),int(x.min()):int(x.max())]
zi = griddata((x, y), z, (xi, yi), method='nearest')
# Plot the figure
plt.imshow(
zi, extent=[x.min(), x.max(), y.min(), y.max()],
origin="lower", interpolation='bicubic', aspect='auto'
)
plt.colorbar()
plt.scatter(x,y, c = 'r')
plt.show()
答案 0 :(得分:2)
根据docs,我的方法是:
# Import libs
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import griddata
from matplotlib.path import Path
from matplotlib.patches import PathPatch
# Create some example data
x = np.array([1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6])
y = np.array([1,2,3,4,2,3,4,5,3,4,5,6,4,5,6,7,5,6,7,8,6,7,8,9])
z = np.linspace(0,100,len(y))
# Grid and interpolate between points
yi, xi = np.mgrid[int(y.min()):int(y.max()),int(x.min()):int(x.max())]
zi = griddata((x, y), z, (xi, yi), method='nearest')
# Plot the figure
im = plt.imshow(
zi, extent=[x.min(), x.max(), y.min(), y.max()],
origin="lower", interpolation='bicubic', aspect='auto',
clip_path=patch, clip_on=True)
plt.colorbar()
path = Path([[1, 1], [1, 4], [6, 9], [6, 6], [1, 1]])
patch = PathPatch(path, facecolor='none')
plt.gca().add_patch(patch)
im.set_clip_path(patch)
要计算您所在区域的拐角,您可以定义一个函数:
def corners(x, y):
xl = np.min(x)
yl = np.min(y)
xh = np.max(x)
yh = np.max(y)
return [[xl, yl], [xl, np.max(y[x==xl])], [xh, yh], [xh, np.min(y[x==xh])], [xl, yl]]
,然后用以下内容替换补丁中的显式点:
...
path = Path(corners(x, y))
...
编辑:
与
patch = PathPatch(path, facecolor='none', edgecolor='none')
您可以纯粹剪辑,而不必显示剪辑路径的边缘。