【问题标题】:Clip off pcolormesh outside of circular set_boundary in Cartopy在 Cartopy 中剪掉圆形 set_boundary 之外的 pcolormesh
【发布时间】:2019-06-18 12:27:33
【问题描述】:

我正在使用 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)

【问题讨论】:

    标签: matplotlib cartopy


    【解决方案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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-03
      • 2013-02-10
      • 2019-12-05
      • 2020-10-08
      • 1970-01-01
      • 2016-04-27
      • 2011-09-25
      • 2021-04-20
      相关资源
      最近更新 更多