Choropleth Maps¶
ChoroplethLayer 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, BasemapLayer, ChoroplethLayer
gdf = gpd.read_file("departements_population.gpkg")
m = Map(gdf, width=800, height="auto")
m.add(BasemapLayer())
m.add(ChoroplethLayer(
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 ChoroplethLayer(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). |
|
|
Render this layer on the zoom viewport. |
|
|
Render this layer on cartouche viewports. |
|
|
Legend sub-config dict. See tables below. |
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). |
|
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). |
|
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. |