无法使用 cartopy 在南极立体地图上显示格网标签

Unable to show grid labels on a Southpolar stereographic map using cartopy

提问人:Rony Golder 提问时间:3/30/2023 更新时间:3/30/2023 访问量:136

问:

我正在尝试在 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()

这将生成带有网格线的地图,但标注不会显示。如何显示网格线的标签?

谢谢你的帮助!

python matplotlib 投影 cartopy 网格线

评论


答:

1赞 Rutger Kassies 3/30/2023 #1

我无法复制这一点。下面的代码片段是一个轻微的修改,可以打开和关闭标签,但对于您的原始代码,它看起来与左侧轴相同。

fig, axs = plt.subplots(1,2, 
    figsize=(8,4), dpi=96, facecolor="w", layout="compressed",
    subplot_kw=dict(projection=ccrs.SouthPolarStereo()),
)

for i, draw_labels in enumerate([True, False]):
    axs[i].set(title=f"{draw_labels=}")
    axs[i].gridlines(
        ccrs.PlateCarree(), draw_labels=draw_labels, linewidth=0.5, 
        color='grey', xlocs = np.arange(-180,180,30),ylocs = np.arange(-85,-30,20),
    )

for ax in axs:
    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)
    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)

enter image description here

评论

0赞 Rony Golder 4/3/2023
谢谢你的努力。您能否修改代码以将网格标签显示为圆形?例如