Skip to content

Open in Colab

Quickstart

basemaplibre draws a MapLibre style under data plotted with matplotlib or GeoPandas, with the parameters of contextily's add_basemap. The style (vector tiles, labels, icons) is rendered for the extent of the axes, at their resolution and directly in their CRS: no GPU, no browser, no raster tiles to merge or warp.

import geopandas

import basemaplibre as bml

A basemap under your data

Plot a GeoDataFrame in Web Mercator (EPSG:3857) and add a basemap, as with contextily.add_basemap. Here, the capitals of Europe from Natural Earth, sized by population. The default style is OpenFreeMap's Liberty.

NE = "https://raw.githubusercontent.com/nvkelso/natural-earth-vector/master/geojson/"
places = geopandas.read_file(NE + "ne_50m_populated_places_simple.geojson")
capitals = places[places.featurecla == "Admin-0 capital"].cx[-12:32, 35:61]

ax = capitals.to_crs(3857).plot(
    figsize=(6, 5.25), color="crimson", markersize=capitals.pop_max / 5e4
)
bml.add_basemap(ax)

png

Another style

source is a MapLibre style: a name of bml.STYLES, the URL of a style JSON, a style dict or a local .json file. The gallery shows all the named ones; they load without an API key, but check the terms of each service before heavy use.

ax = capitals.to_crs(3857).plot(figsize=(6, 5.25), color="gold", markersize=capitals.pop_max / 5e4)
bml.add_basemap(ax, source="OpenFreeMap.Dark")

png

More

  • Attribution: the attributions of the style's sources are written in the corner, wrapped to the axes. Pass attribution="..." to replace them, attribution=False for none, or call bml.add_attribution(ax, text).
  • Cache: downloads (tiles, sprites, glyphs) are cached on disk for the session; the style and its TileJSON are fetched each time, as they name the current tiles. Keep them across sessions with bml.set_cache_dir(path).
  • Other sources: an xyzservices provider or its name, a URL of raster tiles or a raster file is not a MapLibre style. It is drawn by contextily.add_basemap (with a warning), so contextily needs to be installed.