You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Cartopy绘制南极投影地图时无法显示网格标签

南极极射投影地图网格标签不显示的解决方法

问题描述

尝试使用Python的Cartopy库绘制南极极射投影地图,地图可成功绘制,但网格标签无法显示。原代码如下:

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

fig = plt.figure(figsize =(12,8))
ax = plt.axes(projection=ccrs.SouthPolarStereo())
ax.set_extent([-180,180,-90,-30], ccrs.PlateCarree())
ax.add_feature(cfeature.LAND, color='darkgrey')
ax.add_feature(cfeature.OCEAN, color= 'lightblue')
ax.add_feature(cfeature.COASTLINE, linewidth=1.25)
ax.gridlines(ccrs.PlateCarree(), draw_labels=True, linewidth=0.5, 
             color='grey', xlocs = np.arange(-180,180,30),ylocs = np.arange(-85,-30,20))
theta = np.linspace(0, 2*np.pi, 100)
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)
plt.tight_layout()
plt.show()

解决方法

网格标签不显示的核心原因是gridlines()的投影参数与当前轴的南极极射投影不匹配,同时圆形边界可能遮挡标签。可通过以下调整解决:

  • 调整网格标签的显示配置,指定标签的显示位置
  • 微调圆形边界大小,避免遮挡标签
  • 优化标签的旋转设置,适配极射投影

修改后的代码如下:

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

fig = plt.figure(figsize=(12, 8))
ax = plt.axes(projection=ccrs.SouthPolarStereo())
ax.set_extent([-180, 180, -90, -30], ccrs.PlateCarree())

# 添加地图要素
ax.add_feature(cfeature.LAND, color='darkgrey')
ax.add_feature(cfeature.OCEAN, color='lightblue')
ax.add_feature(cfeature.COASTLINE, linewidth=1.25)

# 绘制网格线与标签,配置标签显示规则
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True,
                  linewidth=0.5, color='grey', alpha=0.7,
                  xlocs=np.arange(-180, 181, 30), ylocs=np.arange(-85, -29, 20),
                  rotate_labels=False, label_style={'size': 10})

# 控制标签显示位置,适配南极极射投影
gl.top_labels = False
gl.left_labels = False

# 微调圆形边界半径,避免遮挡外层标签
theta = np.linspace(0, 2*np.pi, 100)
center, radius = [0.5, 0.5], 0.48
verts = np.vstack([np.sin(theta), np.cos(theta)]).T
circle = mpath.Path(verts * radius + center)
ax.set_boundary(circle, transform=ax.transAxes)

plt.tight_layout()
plt.show()

关键改动说明

  1. 标签显示控制:通过gl.top_labels和gl.left_labels关闭不需要的标签,只保留右侧和底部的标签,适配南极极射投影的布局。
  2. 边界调整:将圆形边界半径从0.5缩小到0.48,避免边界遮挡最外层的网格标签。
  3. 标签样式优化:添加rotate_labels=False防止标签旋转导致的显示异常,通过label_style设置合适的字体大小。

内容的提问来源于stack exchange,提问作者Rony Golder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 09:27:31