cartopy自定义边界形状 缺失经纬网

**

问题:研究西南极(160E-60W)区域,使用cartopy出图,gridlines添加经纬网,发现缺失180以西的部分。

**

在这里插入图片描述

import matplotlib.pyplot as plt
import matplotlib.path as mpath
import cartopy
import cartopy.crs as ccrs
import numpy as np
proj = ccrs.Stereographic(central_longitude=228, central_latitude=-70)
fig = plt.figure(figsize=(10,10))
ax = plt.axes([0,0,1,1],projection=proj)
latmin = -90
latmax = -60
lonmin = 150
lonmax = 305
lats = np.linspace(latmax, latmin, latmax - latmin + 1)
lons = np.linspace(lonmin, lonmax, lonmax - lonmin + 1)
vertices = [(lon, latmin) for lon in range(lonmin, lonmax + 1, 1)] + \
    [(lon, latmax) for lon in range(lonmax, lonmin - 1, -1)]
boundary = mpath.Path(vertices)
ax.set_boundary(boundary, transform=ccrs.PlateCarree())
ax.set_extent([170, 320, -90, -60], ccrs.PlateCarree())
ax.add_feature(cartopy.feature.LAND, zorder=1, edgecolor='k')
ax.gridlines(draw_labels=True)
plt.show()

解决方法:cartopy的问题…

参考资料:https://stackoverflow.com/questions/75647121/fail-to-add-gridlines-in-cartopy-custom-shape-boundary-and-stereographic-project

import matplotlib.pyplot as plt
import matplotlib.path as mpath
import cartopy
import cartopy.crs as ccrs
import numpy as np

proj = ccrs.Stereographic(central_longitude=228, central_latitude=-70)
fig = plt.figure(figsize=(10/2, 10/2))
ax = plt.axes([0,0,1,1], projection=proj)

latmin = -90
latmax = -60
lonmin = 150
lonmax = 305
lats = np.linspace(latmax, latmin, latmax - latmin + 1)
lons = np.linspace(lonmin, lonmax, lonmax - lonmin + 1)
vertices = [(lon, latmin) for lon in range(lonmin, lonmax + 1, 1)] + \
    [(lon, latmax) for lon in range(lonmax, lonmin - 1, -1)]

boundary = mpath.Path(vertices)
ax.set_boundary(boundary, transform=ccrs.PlateCarree())

ax.set_extent([170, 320, -90, -60], ccrs.PlateCarree())
ax.add_feature(cartopy.feature.LAND, zorder=1, edgecolor='k')
#ax.add_feature(cartopy.feature.OCEAN, zorder=1, edgecolor='none', alpha=0.3)

# Build gridlines in 2 steps
# First set of gridlines: meridians only
# proper `xlim` is needed to get all the x-labels' visibility set correctly
gl1 = ax.gridlines(draw_labels=True, crs=ccrs.PlateCarree(), \
            xlocs=range(-180, 359, 20), \
            ylocs=[], \
            xlim=[-2847619, 3537282])  # get these from ax.get_xbound() of previous run
            # Second set of gridlines: parallels of latitude only. 
# The lines are terminated at the meridian of the dateline!
gl2 = ax.gridlines(draw_labels=True, crs=ccrs.PlateCarree(), \
            y_inline=True, \
            ylocs=range(-80, -50, 10), \
            xlocs=[])

# Draw the missing parts of the parallel lines at 70, 80 deg_S
# Colors are set to `red` and `green` intentionally
lons = range(-180, -50, 5)
ax.plot(lons, -80*np.ones(len(lons)), transform=ccrs.Geodetic(), lw=0.5, color="red", zorder=30)
ax.plot(lons, -70*np.ones(len(lons)), transform=ccrs.Geodetic(), lw=0.5, color="green", zorder=30)

# Print the bounds of the plot
print("Bounds:", ax.get_xbound(), ax.get_ybound())
plt.show()

请添加图片描述

补充一些我整理的相关资料:

  1. 如何使用cartopy在经过180经线的区域添加经纬线?资料网址: https://stackoverflow.com/questions/59584276/cartopy-set-extent-with-central-longitude-180
  2. cartopy 添加地图经纬线
    https://scitools.org.uk/cartopy/docs/latest/matplotlib/gridliner.html
  3. Making a lat-lon reference plot
    https://climate-cms.org/posts/2018-05-01-latlon-reference.html
  4. 当遇到报错:Failed to determine the required bounds in projection coordinates. Check that the values provided are within the valid range 的解决方法
    资料: Ignore projection limits when setting the extent
    https://stackoverflow.com/questions/54569028/ignore-projection-limits-when-setting-the-extent
  5. cartopy自定义地图边界形状教程:
    https://scitools.org.uk/cartopy/docs/latest/gallery/lines_and_polygons/always_circular_stereo.html#sphx-glr-gallery-lines-and-polygons-always-circular-stereo-py
  6. 使用NOAA OISST数据作图教程:
    https://stackoverflow.com/questions/36525825/mask-ocean-or-land-from-data-using-cartopy
  7. 使用不规则的地图边框形状,导致ax.gridlines()没办法显示经纬线
    http://bbs.06climate.com/forum.php?mod=viewthread&tid=95537&page=2
  8. 使用规则地图边框形状,标注经纬线教程:
    https://nbviewer.org/gist/ajdawson/dd536f786741e987ae4e
  9. 北极巴伦支海自定义地图边框出图教程:
    https://stackoverflow.com/questions/73951145/cartopy-figure-for-high-latitude-with-edges-parallel-to-latitude-and-longitude
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值