Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To display grid labels on a Southpolar stereographic map in Cartopy, you can use the Gridliner class and set the appropriate ticklabels and formatting options. Here is an example code snippet:

import cartopy.crs as ccrs
import matplotlib.pyplot as plt
from cartopy.mpl.gridliner import Gridliner

fig = plt.figure(figsize=(10, 10))

# Define Southpolar Stereographic projection
south_polar_stereo = ccrs.SouthPolarStereo()

# Create subplot with Southpolar Stereographic projection
ax = fig.add_subplot(1, 1, 1, projection=south_polar_stereo)

# Set extent of the plot
ax.set_extent([-180, 180, -90, -60], ccrs.PlateCarree())

# Draw coastlines
ax.coastlines(resolution='110m')

# Create Gridliner object with tick options
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, linestyle='--')
gl.xlabels_top = False
gl.ylabels_right = False
gl.xlabel_style = {'size': 14}
gl.ylabel_style = {'size': 14}

# Set tick formatting options
gl.xformatter = plt.FuncFormatter(lambda lon, _: '{:.0f}°E'.format(lon))
gl.yformatter = plt.FuncFormatter(lambda lat, _: '{:.0f}°S'.format(lat))

plt.show()

In this example, we create a subplot with a Southpolar Stereographic projection using add_subplot(), and set the extent of the plot with set_extent(). We then draw coastlines with coastlines().

To display grid labels, we create a Gridliner object with gridlines() and set the draw_labels option to True. We also set the xlabels_top and ylabels_right options to False to ensure that the labels are displayed at the bottom and left of the plot. We then set the font size of the labels with xlabel_style and ylabel_style.

Finally, we set the formatting of the tick labels using xformatter and yformatter. In this example, we use a lambda function to format the labels with the degrees symbol and the appropriate direction (E for longitude and S for latitude).

The resulting plot will display latitude and longitude grid labels at the bottom and left of the plot, with tick marks and appropriate formatting.