Contextily is a Python library (v1.7.0) for adding context geo-tiles as basemaps to Matplotlib figures or saving them as geospatial raster files. It is actively developed, with minor releases every few months, and is tightly integrated with GeoPandas and the `xyzservices` package for tile providers.
pip install contextilyVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `contextily` with `geopandas` to add a web tile basemap to a geospatial plot. It loads the NYC boroughs dataset, reprojects it to Web Mercator (EPSG:3857) which is standard for web tiles, plots the data, and then overlays a basemap using `cx.add_basemap()`.
Use `contextily.add_basemap()` instead, which offers more flexibility and is the current recommended approach.
If you need a specific tile provider, explicitly specify it using the `source` parameter in `add_basemap`, e.g., `source=cx.providers.CartoDB.Voyager`. For Stamen, you might need an API key and to specify the full URL.
Upgrade your Python environment to version 3.10 or higher.
Before `cx.add_basemap(ax, ...)`, ensure your GeoDataFrame is reprojected (`gdf.to_crs(epsg=3857).plot(ax=ax)`) or explicitly pass `crs=gdf.crs` to `add_basemap`.