Matplotlib geographic maps with CartoPy
PROJ.4 / GEOS-based
CartoPy
downloads and caches shape files as needed, avoiding a large install up front.
CartoPy uses shapely
and other 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()
ax = figure().gca(projection=proj)
ax.add_feature(cpf.LAND)
ax.add_feature(cpf.OCEAN)
ax.add_feature(cpf.COASTLINE)
ax.add_feature(cpf.BORDERS, linestyle=':')
# 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()
CartoPy can even make an auroral oval.
Cartopy replaced Matplotlib basemap, which was deprecated in 2017.