I try to plot a map with cartopy, and label latitudes with some digits.
See the following code.
The last two lines are not effective, and cartopy still displays latitudes as '7.5°S 7°S ...'
, when it should be '7.5°S 7.0°S ...'
import matplotlib.pyplot as plt
import cartopy.mpl.ticker, cartopy.crs as ccrs
# Describe the model projection in the file
ProjIn = ccrs.PlateCarree (central_longitude=0)
# Projection for plots
ProjPlot = ccrs.PlateCarree (central_longitude=0)
# Creates the figure
fig, ax = plt.subplots ( figsize=(15,6), subplot_kw={'projection':ProjPlot} )
ax.set_extent ( ( -60, 10, -10, 10,), crs=ProjIn )
ax.gridlines (draw_labels=True, crs=ProjIn)
ax.coastlines ()
# Format axes
ax.xaxis.set_major_formatter (cartopy.mpl.ticker.LongitudeFormatter(dms=False, number_format='03.1f'))
ax.yaxis.set_major_formatter (cartopy.mpl.ticker.LatitudeFormatter (dms=False, number_format='03.1f'))
Do I miss something ? Is this a bug or a feature ?
Thanks,
Olivier
Seems like you (must?) set the ticks first in order to let the formatting take effect :
ax.set_xticks(np.arange(-60, 10, 10), crs=ccrs.PlateCarree())
ax.set_yticks(np.arange(-10, 10, 2.5), crs=ccrs.PlateCarree())
ax.gridlines(draw_labels=False, crs=ProjIn)
Not sure if it’s a bug, but you can definitely specify the x/y
formatters in the gridlines
: :
import matplotlib.pyplot as plt
import cartopy.mpl.ticker, cartopy.crs as ccrs
proj = ccrs.PlateCarree(central_longitude=0)
fig, ax = plt.subplots(figsize=(15, 6), subplot_kw={"projection": proj})
ax.set_extent((-60, 10, -10, 10), crs=proj)
lon_fmt = cartopy.mpl.ticker.LongitudeFormatter(number_format=".1f")
lat_fmt = cartopy.mpl.ticker.LatitudeFormatter(number_format=".1f")
ax.gridlines(draw_labels=True, crs=proj, xformatter=lon_fmt, yformatter=lat_fmt)
ax.coastlines()
2