Matplotlib 在Cartopy中,在圆形集合_边界外剪掉pcolormesh

Matplotlib 在Cartopy中,在圆形集合_边界外剪掉pcolormesh,matplotlib,cartopy,Matplotlib,Cartopy,我正在使用Cartopy进行极地研究,并希望在我的数据周围剪裁一个圆形边界,我在NorthPolarStereo()投影中绘制该边界。我使用set_extent指示我希望从哪个纬度绘制数据,并使用set_boundary创建圆形边界,如前所述。然后使用matplotlib.pyplot.pcolormesh绘制实际数据。但是,假设我使用set\u extent定义55度的最小纬度,55度以下的一些数据仍在set\u边界之外绘制。如何剪裁这些数据 map_crs = ccrs.NorthPolar

我正在使用Cartopy进行极地研究,并希望在我的数据周围剪裁一个圆形边界,我在
NorthPolarStereo()
投影中绘制该边界。我使用
set_extent
指示我希望从哪个纬度绘制数据,并使用
set_boundary
创建圆形边界,如前所述。然后使用
matplotlib.pyplot.pcolormesh
绘制实际数据。但是,假设我使用
set\u extent
定义55度的最小纬度,55度以下的一些数据仍在
set\u边界之外绘制。如何剪裁这些数据

map_crs = ccrs.NorthPolarStereo(central_longitude=0.0, globe=None)

# Build axes
fig     = plt.figure()
ax      = plt.axes(projection=map_crs)

plotfield = ax.pcolormesh(lons, lats, data, transform=ccrs.PlateCarree())
ax.set_extent((-180, 180, 55, 90), crs=ccrs.PlateCarree())
gl = ax.gridlines()

# Circular clipping
theta = np.linspace(0, 2*np.pi, 400)
center, radius = [0.5, 0.5], 0.5
verts = np.vstack([np.sin(theta), np.cos(theta)]).T
circle = mpath.Path(verts * radius + center)
ax.set_boundary(circle, transform=ax.transAxes)

我没有cartopy在与您相同的条件下对其进行测试,但您可以使用任何形状的面片对象剪裁pcolormesh:

# the code below is adapted from the pcolormesh example
# https://matplotlib.org/3.1.0/gallery/images_contours_and_fields/pcolormesh_levels.html#sphx-glr-gallery-images-contours-and-fields-pcolormesh-levels-py

# make these smaller to increase the resolution
dx, dy = 0.05, 0.05

# generate 2 2d grids for the x & y bounds
y, x = np.mgrid[slice(1, 5 + dy, dy),
                slice(1, 5 + dx, dx)]
z = np.sin(x)**10 + np.cos(10 + y*x) * np.cos(x)

theta = np.linspace(0, 2*np.pi, 400)
center, radius = [0.5, 0.5], 0.5
verts = np.vstack([np.sin(theta), np.cos(theta)]).T
circle = matplotlib.path.Path(verts * radius + center)

fig, ax = plt.subplots()
im = ax.pcolormesh(x, y, z, cmap='viridis', clip_path=(circle, ax.transAxes))

fig.colorbar(im, ax=ax)
fig.tight_layout()

plt.show()