Choropleth Maps¶
Choropleth classifies a numeric column using
mapclassify and draws filled polygons. The legend is drawn automatically
below the main map.
import geopandas as gpd
from mappyng import Map, Basemap, Choropleth
gdf = gpd.read_file("departements_population.gpkg")
m = Map(gdf, width=800, height="auto")
m.add(Basemap())
m.add(Choropleth(
gdf,
column="pop",
cmap="YlOrRd",
method="Quantiles",
num_classes=5,
stroke_width=0.15,
legend={
"title": "Population",
"decimals": 0,
"orientation": "vertical",
},
))
m.render("choropleth.svg")
Layer parameters¶
These are passed as keyword arguments to Choropleth(gdf, ...).
Key |
Default |
Description |
|---|---|---|
|
required |
Numeric column to classify. |
|
|
Colormap name ( |
|
|
Classification scheme: |
|
5 |
Number of classes (4-7). Ignored when |
|
from style |
Polygon outline color. |
|
from style |
Polygon outline width in SVG px. |
|
|
Dissolve polygons by class before rendering (reduces SVG size). |
|
|
Make the borders between neighbours of similar colour visible
( |
|
|
Render this layer on the zoom viewport. |
|
|
Render this layer on cartouche viewports. |
|
|
Legend sub-config dict. See tables below. |
Borders between similar colours¶
When two touching areas are both light, or both dark, and the outline is close
to them in tone, the border between them disappears and the two areas read as
one. border_contrast finds those shared borders and redraws only them,
in a colour that stands out, on top of the polygons. The polygons keep their own
outline, and every other border is left as it is.
mp.Choropleth(gdf, column="rate", cmap="Blues", stroke="#eeeeee",
border_contrast=True)
The gallery compares the three states side by side: Borders between similar colours.
Checking first. border_contrast="check" does not change the map’s look,
it reveals the problem: every weak border is drawn thick in magenta and a
warning gives their number and the first pairs of areas concerned. Once you have
seen which borders are hard to read, switch to True to fix them, or keep
"check" as a quick audit of a palette.
How a border is judged. The contrast ratio is the WCAG one, from 1 (same tone) to 21 (black on white). A shared border is weak, and gets redrawn, when both conditions hold:
its two fills are close (ratio below
fill_ratio): the change of colour alone does not show the border;the outline is close to at least one of the fills (ratio below
stroke_ratio): the outline does not show it either.
Two fills of the same class are the extreme case (ratio 1). A border between a
light and a dark area is never touched: the colour change is enough. For a weak
border, light and dark are tried, and the one that contrasts most with
the two fills is used, provided it does better than the current outline. So two
pale areas get a dark line and two dark areas a light one.
Options. True or "fix" use the defaults. A dict, or a
BorderContrast, sets any of them:
Option |
Default |
Meaning |
|---|---|---|
|
|
|
|
1.5 |
Fills with a contrast ratio below this are alike. Raise it to also treat moderately different neighbours as weak. |
|
2.0 |
An outline with a ratio below this against a fill does not show. |
|
|
The two colours tried for a weak border. |
|
|
Colour of the weak borders in |
|
1.0 |
Width of a redrawn border relative to the outline width. |
mp.Choropleth(gdf, column="rate", stroke="#ffffff",
border_contrast={"dark": "#003366", "width_factor": 1.5})
Good to know.
It applies to the main map, the zoom and the cartouches, each judged on what it draws.
Categoryaccepts the same option, for categories with close colours.The shared borders are found from the polygons’ own geometry, so adjacent areas must share their boundary exactly, as administrative layers do. Gaps or overlaps between neighbours hide the border from the search. With
simplify, the redrawn lines are simplified in the same way.The colours are the nominal fill colours. With
opacity_columnthe rendered tone also depends on what lies under the area, unless you setblend_background; the check uses the blended colour when you do.A map with no outline (
stroke="none") counts as having a fully transparent outline: neighbours of similar colour are all weak.The redrawn borders ignore the pointer, so hover tooltips of an interactive export still reach the areas below.
Areas without data (hatched) are not part of the check.
The option is plain data, so it survives
to_yaml.
Legend parameters (legend sub-dict)¶
Choropleth-specific fields¶
Key |
Default |
Description |
|---|---|---|
|
1 |
Decimal places for class boundary labels. |
|
|
Legend layout: |
|
|
Global width override for all swatches (horizontal and vertical
modes). Height is always derived as |
|
30 |
Color swatch width in horizontal mode (SVG px). |
|
auto |
Swatch height in horizontal mode.
Defaults to |
|
2 |
Gap between swatches (SVG px). |
|
|
Swatch shape: |
|
auto |
Corner radius of |
|
0 |
Label rotation in degrees. |
|
|
|
|
|
With |
|
|
SVG text-anchor for rotated labels: |
|
4 |
Gap between swatch bottom and label baseline (SVG px). |
|
|
Label for missing-value swatch. |
|
10 |
Gap between last class swatch and N/A swatch (SVG px). |
|
|
Vertical legend: |
|
4 |
Gap between N/A swatch right edge and its label (SVG px). |
|
16 |
Swatch width in vertical mode (SVG px). |
|
12 |
Swatch height in vertical mode (SVG px). |
|
6 |
Gap between swatch and label in vertical mode (SVG px). |
|
|
Custom label list (length == num_classes) replacing auto-generated labels. |
Legend frame fields (frame dict)¶
Key |
Default |
Description |
|---|---|---|
|
|
Frame background color. |
|
0.9 |
Frame background opacity. |
|
|
Frame border color. |
|
0.5 |
Frame border width (SVG px). |
|
4 |
Frame corner radius (SVG px). |
|
8 |
Inner padding between content and frame edge (SVG px). |
|
|
Add drop shadow behind the frame. |
|
2 / 2 |
Shadow offset (SVG px). |
|
4 |
Shadow blur radius (SVG px). |
|
0.3 |
Shadow opacity. |