How to make Cartopy gridline labels show with Lambert Conformal projection?

Viewed 718

I am trying to plot a map in a Lambert Conformal Projection, using Cartopy version 0.18. In the code below, I define my projection and plot the axes. I then add gridlines and grid labels. Because of the Lambert Conformal projection the default is for the longitude labels to be shown on the map itself, which is something I did not desire, so used the x_inline=False, y_inline=False arguments to make the labels show on the bottom. I then wanted to specify the x and y tick locations that I wanted the grid lines and labels shown at. You can see I specify gl.ylocator = ticker.FixedLocator([25,30,35,40, 45, 50]) and gl.xlocator = ticker.FixedLocator([-100, -95, -90, -85, -80, -75, -70]) but on the map created it randomly doesn't show some of the labels. Is there something I am not considering, or an argument I need to pass in order to display the labels at the locations I specify? I know plotting grid labels with Lambert Conformal projection is a new feature in Cartopy, so I wasn't sure if this is just a caveat or if there is a fix.

import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
from matplotlib import ticker
import proplot

proj = ccrs.LambertConformal(central_longitude=-87.5, central_latitude=32.5)
ax = plt.axes(projection=proj)
ax.set_extent([-100,-70,24,50], ccrs.PlateCarree())
ax.add_feature(cfeature.OCEAN.with_scale('10m'),facecolor='paleturquoise',alpha=0.4)
ax.add_feature(cfeature.STATES.with_scale('10m'),facecolor='olivedrab',alpha=0.4)
ax.add_feature(cfeature.STATES.with_scale('10m'),edgecolor='black')
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, x_inline=False, y_inline=False, linewidth=0.33, color='k',alpha=0.5)
gl.right_labels = gl.top_labels = False
gl.ylocator = ticker.FixedLocator([25,30,35,40, 45, 50])
gl.xlocator = ticker.FixedLocator([-100, -95, -90, -85, -80, -75, -70])
plt.show()

Map Example Output

2 Answers

The labels accompanying the gridlines are NOT ramdomly created/plotted. They are created and checked if each individual can be plotted without clashing the existing objects. If enough spaces are provided, and no clashing found, all the labels will be plotted. Sometimes, setting the figsize with larger dimensions can solve the problem.

import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
from matplotlib import ticker
#import proplot

plt.figure(figsize=[12,12])

proj = ccrs.LambertConformal(central_longitude=-87.5, central_latitude=32.5)
ax = plt.axes(projection=proj)
ax.set_extent([-100,-70,24,50], ccrs.PlateCarree())
res = '110m' #'50m', '110m'
ax.add_feature(cfeature.OCEAN.with_scale(res),facecolor='paleturquoise',alpha=0.4)
ax.add_feature(cfeature.STATES.with_scale(res),facecolor='olivedrab',alpha=0.4)
ax.add_feature(cfeature.STATES.with_scale(res),edgecolor='black')
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, x_inline=False, y_inline=False, linewidth=0.33, color='k',alpha=0.5)
gl.right_labels = gl.top_labels = False
gl.ylocator = ticker.FixedLocator([25,30,35,40, 45, 50])
gl.xlocator = ticker.FixedLocator([-100, -95, -90, -85, -80, -75, -70])
plt.show()

plot1

I used your code, removing the 10 m resolution and the import proplot and got what I think you want.

import matplotlib.pyplot as plt
from matplotlib import ticker

proj = ccrs.LambertConformal(central_longitude=-87.5, central_latitude=32.5)
ax = plt.axes(projection=proj)
ax.set_extent([-100,-70,24,50], ccrs.PlateCarree())
ax.add_feature(cfeature.OCEAN,facecolor='paleturquoise',alpha=0.4)
ax.add_feature(cfeature.STATES,facecolor='olivedrab',alpha=0.4)
ax.add_feature(cfeature.STATES,edgecolor='black')
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, x_inline=False, y_inline=False, linewidth=0.33, color='k',alpha=0.5)
gl.right_labels = gl.top_labels = False
gl.ylocator = ticker.FixedLocator([25,30,35,40, 45, 50])
gl.xlocator = ticker.FixedLocator([-100, -95, -90, -85, -80, -75, -70])

enter image description here

Related