我正在使用Cartopy进行极地研究,并想在我的数据周围修剪一个圆形边界,该边界是在NorthPolarStereo()
投影中绘制的。我使用set_extent
来指示要从哪个纬度绘制数据,并使用set_boundary
来创建圆形边界,如in the gallery所述。然后,我使用matplotlib.pyplot.pcolormesh
绘制实际数据。但是,假设我使用set_extent
定义了55度的最小纬度,我的某些低于55度的数据仍被绘制在set_boundary
之外。如何截取这些数据?
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)
答案 0 :(得分:1)
我没有Cartopy在与您相同的条件下对其进行测试,但是您可以使用任何形状的Patch对象来裁剪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()