matplotlib 无法使用制图在南极立体Map上显示格网标注

nkhmeac6  于 2023-03-30  发布在  其他
关注(0)|答案(1)|浏览(118)

我尝试在Python中使用cartopy绘制极坐标立体图。我能够成功绘制Map,但在显示网格标签时遇到问题。
下面是我的代码:

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()

这将生成一个带有网格线的Map,但未显示标签。如何显示网格线的标签?
谢谢你的帮助!

iqjalb3h

iqjalb3h1#

我不能复制这个。下面的代码片段是一个轻微的修改,切换标签和关闭,但与您的原始代码,它看起来一样的左轴对我来说。

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)

相关问题