Search code examples
pythonmatplotlibprojectioncartopygridlines

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


I am trying to plot a polar stereographic map using cartopy in Python. I am able to plot the map successfully, but I am having trouble showing the grid labels.

Here is my code:

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

This produces a map with gridlines, but the labels are not shown. How can I show the labels for the gridlines?

Thank you for your help!


Solution

  • I can't replicate this. The snippet below is a slight modification which toggles the labels on and off, but with your original code it looks the same as the left axes for me.

    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