Matplotlib geographic maps with CartoPy
PROJ.4 / GEOS-based CartoPy downloads and caches shape files as needed, avoiding a large install up front. CartoPy is an alternative to Matplotlib basemap. CartoPy uses easily available / automatically & seamlessly installed prereqs.
conda install cartopy
Note the zorder
option of Matplotlib elements such as contourf
.
Higher number zorder
is higher priority (on top).
See
PlotPrecip.py
for an example of zorder
.
An example using CartoPy follows:
import cartopy
import cartopy.feature as cpf
from matplotlib.pyplot import figure, show
import numpy.random as npr
proj = cartopy.crs.PlateCarree()
fig = figure()
ax = fig.add_subplot(projection=proj)
ax.add_feature(cpf.LAND) # type: ignore
ax.add_feature(cpf.OCEAN) # type: ignore
ax.add_feature(cpf.COASTLINE) # type: ignore
ax.add_feature(cpf.BORDERS, linestyle=':') # type: ignore
# ax.add_feature(cpf.LAKES, alpha=0.5)
# ax.add_feature(cpf.RIVERS)
N = 10
lat = (npr.random(N) - 0.5) * 180
lon = (npr.random(N) - 0.5) * 360
ax.scatter(lon, lat, transform=proj)
show()
The #type: ignore
is to avoid mypy lint as
CartoPy
didn’t yet have type hinting support.
CartoPy can even make an auroral oval.