Skip to content

Open in Colab

Quickstart

A bivariate choropleth shows two variables at once: each has an axis of colours, and the mix of both says where they are high together, low together, or apart.

import basemaplibre as bml
import geopandas as gpd
import matplotlib.pyplot as plt
from mapclassify import Quantiles

from bivario import explore_bivariate_data, get_bivariate_cmap, plot_bivariate_legend
from bivario.example_data import nyc_bike_trips

An interactive map

The example data has bike trips in New York, counted in H3 cells. Where do people start their morning trips, and where do they end them?

trips = nyc_bike_trips()
trips.head()
h3 morning_starts morning_ends afternoon_starts afternoon_ends geometry
0 617733120577503231 903 440 760 803 POLYGON ((-73.88768 40.876, -73.88979 40.87506...
1 617733120577765375 908 342 1259 1695 POLYGON ((-73.88544 40.87341, -73.88755 40.872...
2 617733120578027519 366 860 1180 775 POLYGON ((-73.89196 40.87589, -73.89408 40.874...
3 617733120578289663 1848 1338 2768 2401 POLYGON ((-73.88972 40.8733, -73.89183 40.8723...
4 617733120578551807 1387 759 2208 2456 POLYGON ((-73.88563 40.8787, -73.88774 40.8777...
explore_bivariate_data(trips, "morning_starts", "morning_ends")

Values are split into five classes by natural breaks. Labels, the number of classes, the palette and the dark mode are all parameters:

explore_bivariate_data(
    trips,
    "morning_starts",
    "morning_ends",
    column_a_label="Starts",
    column_b_label="Ends",
    scheme="Quantiles",
    k=3,
    cmap="bubblegum",
    dark_mode=True,
)

More of it in the Folium and Lonboard examples.

Your own data

Any GeoDataFrame with two columns of numbers works. Here are the countries of Europe from Natural Earth: how densely populated they are, and how rich.

url = (
    "https://raw.githubusercontent.com/nvkelso/natural-earth-vector/master/geojson/"
    "ne_50m_admin_0_countries.geojson"
)
countries = gpd.read_file(url, columns=["NAME", "CONTINENT", "POP_EST", "GDP_MD"])
countries = countries[countries["POP_EST"] > 100_000]
europe = countries[countries["CONTINENT"] == "Europe"].to_crs(3035)
europe["density"] = europe["POP_EST"] / (europe.area / 1e6)
europe["gdp_per_capita"] = europe["GDP_MD"] * 1e6 / europe["POP_EST"]
europe.sort_values("gdp_per_capita", ascending=False).head()
CONTINENT GDP_MD NAME POP_EST geometry density gdp_per_capita
119 Europe 71104 Luxembourg 619896.0 POLYGON ((4043391.152 3008317.537, 4042649.392... 236.542021 114703.111490
47 Europe 703082 Switzerland 8574832.0 POLYGON ((4285130.664 2712404.721, 4287410.954... 207.541334 81993.676378
140 Europe 388698 Ireland 4941444.0 MULTIPOLYGON (((3029168.612 3602630.766, 30280... 71.634361 78660.812507
88 Europe 403336 Norway 5347896.0 MULTIPOLYGON (((4748119.801 5133943.092, 47430... 13.825666 75419.566873
145 Europe 24188 Iceland 361313.0 POLYGON ((3193359.76 5001486.979, 3197984.296 ... 3.548561 66944.726594

A static map

A colourmap takes the two columns and returns a colour for every row, which is what GeoDataFrame.plot needs. Here the values are first classified into three groups of the same size:

density = Quantiles(europe["density"], k=3)
gdp = Quantiles(europe["gdp_per_capita"], k=3)

cmap = get_bivariate_cmap("plum_mint")
colours = cmap(density.yb, gdp.yb)
colours[:3]
array([[ 0.12001613,  0.1399997 ,  0.40000098],
       [ 0.12001613,  0.1399997 ,  0.40000098],
       [ 0.81190869,  0.66226763,  0.77697194]])
fig, ax = plt.subplots(figsize=(11, 9), dpi=150)
ax.set_axis_off()
ax.set_xlim(1_900_000, 6_000_000)
ax.set_ylim(1_400_000, 4_700_000)

europe.plot(ax=ax, color=colours, alpha=0.85, edgecolor="white", linewidth=0.5)
bml.add_basemap(ax, crs=europe.crs, source="OpenFreeMap.Positron", labels="above")

plot_bivariate_legend(
    europe["density"],
    europe["gdp_per_capita"],
    ax=ax.inset_axes((0.07, 0.74, 0.22, 0.22)),
    cmap=cmap,
    grid_size=3,
    label_a="People per km²",
    label_b="GDP per capita [$]",
    tick_labels_a=[f"{value:,.0f}" for value in (europe["density"].min(), *density.bins)],
    tick_labels_b=[f"{value:,.0f}" for value in (europe["gdp_per_capita"].min(), *gdp.bins)],
    tick_fontsize_px=15,
)
plt.show()

png

The basemap is drawn by basemaplibre in the CRS of the data, with its labels above the countries. The Matplotlib example shows other layouts, the colourmaps one how to make your own palette.