Cartopy
Cartopy is a Python package designed for geospatial data processing and cartographic visualizations, building on Matplotlib. It provides object-oriented projection definitions, transformations between various map projections, and powerful vector data handling. The library is currently at version 0.25.0 and maintains an active release cadence, frequently updating to support modern Python and geospatial libraries.
Warnings
- breaking Python 2 support was removed in Cartopy v0.19. Users on older Python versions must upgrade to Python 3.7+ (v0.20) or 3.10+ (v0.25).
- breaking The `Axes.natural_earth_shp()` method has been removed. Use `Axes.add_feature()` along with classes from the `cartopy.feature` module instead.
- deprecated Gridliner label control attributes like `Gridliner.xlabels_top`, `xlabels_bottom`, `ylabels_left`, and `ylabels_right` are removed. They were deprecated in v0.18 and removed in v0.23.
- breaking The `cartopy.mpl.clip_path` module is deprecated without replacement, and `path_to_geos`/`geos_to_path` are deprecated in favor of `path_to_shapely`/`shapely_to_path`. This affects low-level path manipulation.
- gotcha Installing Cartopy via `pip` can be challenging due to its complex external (GEOS, PROJ) and Python (Shapely, pyproj) dependencies, especially on Windows or when pre-built wheels are unavailable. Conda is often recommended for a smoother installation.
- gotcha From v0.20, Cartopy uses `pyproj` for transformations, which can sometimes be slower. An environment variable can improve performance.
- gotcha Internal function calls in Cartopy (e.g., for geometries) now prefer `(longitude, latitude)` ordering for consistency.
Install
-
pip install cartopy -
conda install -c conda-forge cartopy
Imports
- crs
import cartopy.crs as ccrs
- feature
import cartopy.feature as cfeature
- GeoAxes
from cartopy.mpl.geoaxes import GeoAxes
Quickstart
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())
ax.set_global()
# Add basic features
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.COASTLINE)
ax.add_feature(cfeature.BORDERS, linestyle=':')
ax.add_feature(cfeature.LAKES, alpha=0.5)
ax.add_feature(cfeature.RIVERS)
plt.title('Basic Cartopy Map with Plate Carree Projection')
plt.show()