从scipy.interpolate.griddatadocumentation 的这一部分判断,griddata 似乎在您的数据点周围的凸包上进行插值:
fill_value : float,可选 用于填写请求的值
输入点的凸包外的点。如果不
提供,则默认为 nan。此选项对
“最近”方法。
这意味着您总是将获得您提供给griddata 的点之外的数据。为了不显示这些区域,您可以将相应点设置为无效 (np.nan) 值。在你的情况下,这很简单。
from matplotlib import pyplot as plt
from scipy.interpolate import griddata
import numpy as np
newPress=np.asarray([22.640048521269733, 8.7880990280388946, 8.5228130097742358, 6.1368312788828003, -0.012232139892299099,
-0.0085282865280444931, 1.4163311525005766, 0.62047309770660242, 14.472422590937441, 15.268280645731416,
17.653997267541644, 24.760479124815305, 22.374762503005076, 22.640048521269733])
poly3 = np.asarray([
(
-15.394, -15.394, -14.394, -14.394, 8.784995481927707,
12.394, 12.394, 15.394, 15.394, 12.394, 12.394, -14.394,
-14.394, -15.394
),
(
13.0625, -13.0625, -13.0625, -17.5625, -17.5625, -15.74980786686838,
-13.0625, -13.0625, 13.0625, 13.0625, 17.562, 17.562, 13.0625, 13.0625
)
])
numcols, numrows = 200, 200
xi = np.linspace(min(poly3[0]), max(poly3[0]), numcols)
yi = np.linspace(min(poly3[1]), max(poly3[1]), numrows)
x, y, z = poly3[0], poly3[1], newPress
xi, yi = np.meshgrid(xi, yi)
zi = griddata(poly3.T,z.T,np.asarray([xi,yi]).T, method='linear').T
fig2 = plt.figure(figsize=(8, 3.5))
ax2 = fig2.add_subplot(111)
ax2.scatter(x,y)
##finding the corners:
ll,lr,ur,ul = zip(x[[2,6,9,12]],y[[2,6,9,12]])
##removing data:
zi[np.logical_and(xi<ll[0],yi<ll[1])] = np.nan
zi[np.logical_and(xi>lr[0],yi<lr[1])] = np.nan
zi[np.logical_and(xi>ur[0],yi>ur[1])] = np.nan
zi[np.logical_and(xi<ul[0],yi>ul[1])] = np.nan
m = ax2.pcolormesh(xi, yi, zi, alpha=0.15, cmap='viridis_r')
plt.show()
结果如下:
请注意,您的示例代码无法运行,需要进行一些调整。下次当您提出问题时,请确保您的代码是Minimal, Complete, and Verifiable example。
编辑:
对于任意形状的多边形,您可以使用我在here 概述的技术,在其中定义由多边形和绘图区域轮廓组成的路径,然后用白色覆盖 pcolormesh 绘图.请注意,为了使其正常工作,您的多边形边缘必须正确排序沿着多边形的轮廓(我在下图中的左侧子图中指出了这一点)。在下面的示例中,边缘按数学正方向(逆时针)排序,绘图区域的边缘按相反方式(顺时针)排序:
from matplotlib import pyplot as plt
from scipy.interpolate import griddata
import numpy as np
from matplotlib.patches import Path, PathPatch
##generate an example polygon:
n_edges = 10
x_max = 15
y_max = 15
##note the sorting of the theta values
thetas = 2*np.pi*np.sort(np.random.rand(n_edges))
radii = 0.5*(np.random.rand(len(thetas))+1)
x = np.cos(thetas)*x_max*radii
y = np.sin(thetas)*y_max*radii
values = np.random.rand(len(thetas))
fig, axes = plt.subplots(ncols=2)
##visualisation
axes[0].quiver(
x[:-1],y[:-1],x[1:]-x[:-1],y[1:]-y[:-1],
scale_units='xy',angles='xy',scale=1,
lw = 3
)
axes[0].scatter(x,y,c=values,zorder=10,cmap='viridis_r')
##interpolation:
numcols, numrows = 200, 200
xi = np.linspace(min(x), max(x), numcols)
yi = np.linspace(min(y), max(y), numrows)
z = values
poly3 = np.asarray([x,y])
xi, yi = np.meshgrid(xi, yi)
zi = griddata(poly3.T,z.T,np.asarray([xi,yi]).T, method='linear').T
axes[1].scatter(x,y, zorder=10)
m = axes[1].pcolormesh(xi, yi, zi, alpha=0.15, cmap='viridis_r',zorder=8)
##getting the limits of the map:
x0,x1 = axes[1].get_xlim()
y0,y1 = axes[1].get_ylim()
map_edges = np.array([[x0,y0],[x0,y1],[x1,y1],[x1,y0]])
##masking the outsides of the polygon
mask = [map_edges,poly3.T]
codes = [[Path.MOVETO] + [Path.LINETO for p in m[1:]] for m in mask]
codes = np.concatenate(codes)
verts = np.concatenate(mask)
path = Path(verts,codes)
patch = PathPatch(path,facecolor='white', lw=0,zorder=9)
axes[1].add_patch(patch)
plt.show()
结果如下所示: