Reputation: 717
I'm plotting a NorthPolarStereo map with the following code. I'd like to label the latitude and longitude gridlines, but Cartopy only seems to be placing these labels on the top/bottom of the plot, and I'd like them to go all around. I know something like this must be possible because of this related SO question: Setting longitude of latitude tick labels in NorthPolarStereo Cartopy but I can't seem to reproduce it.
Also, is there any way to customize where the inline y (latitude) labels are? They are partially obscured by the gridlines and the coastline features.
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import numpy as np
import matplotlib.ticker as mticker
from matplotlib.offsetbox import AnchoredText
fig = plt.figure(figsize=(5,5))
projection = ccrs.NorthPolarStereo(central_longitude=-100)
ax = plt.subplot(projection=projection)
ax.set_extent([0, 360, 65, 90], crs=ccrs.PlateCarree())
ax.add_feature(cfeature.COASTLINE)
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.LAND)
xticks = np.arange(-180, 181, 30)
yticks = np.arange(70, 91, 10)
gl = ax.gridlines(crs=ccrs.PlateCarree(), color='k', draw_labels=True, dms=True, x_inline=False, y_inline=True)
gl.ylocator = mticker.FixedLocator(yticks)
gl.xlocator = mticker.FixedLocator(xticks)
gl.xlabel_style = {'rotation':0}
text = AnchoredText('© Natural Earth; license: public domain',
loc=4, prop={'size': 10}, frameon=True)
ax.add_artist(text)
plt.show()
And the resulting image:
Upvotes: 4
Views: 2440
Reputation: 18782
I use cartopy version 0.19.0 and have no issue about the gridlines' labels. With minor changes on your code, I run and get the gridlines' lables on all sides.
For the clash between labels and map features that causes the labels difficult to read, cartographers have several choices to solve the problem. They usually use color and style of the plots.
The modified code and the plot demonstrate all the above.
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import numpy as np
import matplotlib.ticker as mticker
from matplotlib.offsetbox import AnchoredText
fig = plt.figure(figsize=(5,5))
projection = ccrs.NorthPolarStereo(central_longitude=-100)
ax = plt.subplot(projection=projection)
ax.set_extent([-180, 179.9, 65, 90], ccrs.PlateCarree())
#ax.set_extent([0, 360, 65, 90], crs=ccrs.PlateCarree())
ax.add_feature(cfeature.COASTLINE, color="gray", lw=0.75)
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.LAND)
xticks = np.arange(-180, 181, 30)
yticks = np.arange(70, 91, 10)
gl = ax.gridlines(crs=ccrs.PlateCarree(),
color='gray',
draw_labels=True,
dms=True,
x_inline=False,
y_inline=True)
gl.ylocator = mticker.FixedLocator(yticks)
gl.xlocator = mticker.FixedLocator(xticks)
gl.xlabel_style = {'rotation':0}
text = AnchoredText('© Natural Earth; license: public domain',
loc=4, prop={'size': 10}, frameon=True)
ax.add_artist(text)
plt.show()
Edit From the comment below, it is found that cartopy v0.21.0 does not produce the expected plot.
I dont know the reason why, but it is possible to work around and get the result by replacing plt.show()
with this code.
import cartopy
if cartopy.__version__ == '0.21.0':
plt.draw()
for ea in gl.left_label_artists+gl.right_label_artists:
ea.set_visible(True)
plt.show()
Upvotes: 2