Skip to content

Thematic maps & legends

A thematic map colours (or sizes) each feature by one of its values: income by tract, population by city, land use by parcel. In MapLibre you write that as a step or match expression, and then write the legend by hand to match it. A classify: block on a layer does both from one description of the classes. This page is also where legends are explained, for classified layers and any other.

The examples use two sample files: sample-tracts.geojson (made-up census tracts in Philadelphia) and world-cities.geojson. Download them into data/classify/ in your site (public/data/classify/ in Astro) so the url: paths below work. Then:

type: map
id: classify-choropleth
config:
center: [-75.19, 39.936]
zoom: 12
mapStyle: "https://tiles.openfreemap.org/styles/positron"
sources:
tracts:
type: geojson
url: /data/classify/sample-tracts.geojson
layers:
- id: income
type: fill
source: tracts
label: Median household income
classify:
field: median_income # the feature property to classify
method: quantile # breaks computed from the data
classes: 5
palette: Blues # a ColorBrewer palette
format: { currency: USD, max-fraction-digits: 0 }
paint:
fill-opacity: 0.85
fill-outline-color: "#ffffff"
Five quantile classes computed when the data loads; tracts without a value are light grey. Click a tract for its value.

Edit this map in the playground

The tracts are shaded in five blues, and a legend in the top-left corner lists each class with its dollar range. The basemap is OpenFreeMap’s positron, a quiet grey one that keeps the classes readable.

classify: sits on the layer beside paint: and compiles to the step (numeric classes) or match (categories) expression you would otherwise write, in the layer’s main colour (fill-color, circle-color …). The terms here (breaks, palettes, no data) mean what they mean in MapLibre, QGIS and d3. Thematic maps in the examples shows each map on this page with its complete document, and the gallery’s Visualize population density computes a rate with plain expressions.

classify:
field: heat_index
breaks: [0.2, 0.4, 0.6, 0.8] # 4 breaks → 5 classes
palette: YlOrRd

breaks: are MapLibre step thresholds: n breaks make n + 1 classes, and a value equal to a break goes in the class above it (0.4 is in the third class, 0.4–0.6). The first class is everything below the first break, the last everything from the last break up.

Explicit breaks work on every source, GeoJSON, vector tiles and PMTiles alike, because nothing has to be computed in the browser. Breaks you computed elsewhere (QGIS, a notebook, a statistics office’s published ranges) go here.

classify:
field: median_income
method: quantile # quantile | equal | jenks
classes: 5 # default 5
method:ClassesGood for
quantilethe same number of features in each classranking; skewed data
equalthe same width of value range in each classevenly spread data; values with a natural unit
jenksnatural breaks: groups that are as alike inside, and as different between, as possible (Fisher–Jenks, computed exactly)data with clusters

The breaks are computed when the data arrives: at load for inline data:, when the file loads for a url:, and again on every refresh of live data. The classes always describe the features on the map. When many values tie (40% of tracts at 0), a method can produce fewer classes than you asked for; the map and legend show the classes that exist.

method: needs every feature’s value. A GeoParquet file read by view (load: viewport, or a large file the map reads by view by itself) has it: when the source loads, the map reads the classified column from every full-detail row, computes the breaks once, and keeps them as you pan and zoom. That read is capped at 32 MB compressed per column; over it, the layer asks for breaks: in its notice.

A vector tile source (including PMTiles and the basemap’s sources) only ever holds the tiles in view, and so does a FlatGeobuf file read by view; breaks computed there would change as you pan, so method: is refused on them. Compute the breaks from the whole local file with mlym breaks and paste the breaks: line it prints (Class breaks on large data):

Terminal window
npx maplibre-yaml@latest breaks tracts.fgb --field median_income --method quantile --classes 5

Validation refuses method: on a vector source or a FlatGeobuf with load: viewport. A FlatGeobuf file over 50 MB switches to reading by view by itself, so for one of those the map checks when it reads the file and asks for breaks: in its notice (FlatGeobuf).

classify:
field: land_use
categories:
residential: "#f6e8c3"
commercial: "#d8b365"
industrial: "#8c510a"
other: "#bbbbbb" # values you did not list (default grey; or hide)

Or list the values and let a palette colour them, in order:

classify:
field: CONTINENT
categories: [Africa, Asia, Europe, North America, South America, Oceania]
palette: okabe-ito
other: "#d9d9d9"

Categories compile to a match. Numbers work too (categories: [1, 2, 3] matches a zoning code stored as 1 or "1"). The legend lists categories in the order you write them. Categories need no data scan, so they work on vector tiles exactly as on GeoJSON:

The demo tiles' countries by continent, with a colour-blind-safe palette

Edit this map in the playground

palette: names a palette; colors: lists your own (one per class, or per category). Names are matched case-insensitively (blues is Blues), and a near miss gets a “did you mean”.

KindPalettes
Sequential (light → dark)Blues Greens Greys Oranges Purples Reds BuGn BuPu GnBu OrRd PuBu PuBuGn PuRd RdPu YlGn YlGnBu YlOrBr YlOrRd viridis cividis
Diverging (low ↔ high around a middle)BrBG PiYG PRGn PuOr RdBu RdGy RdYlBu RdYlGn Spectral
Qualitative (categories)okabe-ito Set1 Set2 Set3 Pastel1 Pastel2 Dark2 Paired Accent
  • Colour-blind safety. Every sequential palette, viridis, cividis, okabe-ito, and the diverging BrBG PiYG PRGn PuOr RdBu (and RdYlBu at small class counts) stay distinguishable for the common colour-vision deficiencies. RdYlGn, Spectral and the ColorBrewer qualitative sets do not; prefer okabe-ito for categories.
  • Defaults. Numeric classes use YlGnBu (viridis past 9 classes); categories use okabe-ito. Both are colour-blind safe.
  • Class counts. ColorBrewer palettes start at 3 classes and go up to 9 (sequential), 11 (diverging) or 8–12 (qualitative); viridis and cividis go to 12, okabe-ito to 8. Validation says when a palette has too few colours.
  • reverse: true flips a palette (dark → light).

On a circle layer, radius: sizes each class; the same breaks drive circle-color and circle-radius:

- id: cities
type: circle
source: cities
label: City population
classify:
field: pop
method: jenks
classes: 5
palette: YlOrRd
radius: [2, 4, 7, 10, 14] # one circle-radius per class
Natural-breaks classes; the legend draws each class's circle at its size

Edit this map in the playground

Leave out palette:/colors: and only the radius is classified (the layer’s own circle-color stays); features with no value are then hidden.

A map of raw counts mostly shows where people live: the biggest tracts have the most of everything. Map a rate instead. normalize: divides the field by another property, and multiply: scales the result:

classify:
field: cases # the count
normalize: population # ÷ the people it happened to
multiply: 100000 # × 100,000 → cases per 100,000 residents
method: jenks # breaks are computed on the rate
classes: 5
palette: OrRd
format: { max-fraction-digits: 0 }
Cases per 100,000 residents: the legend's edges are rates

Edit this map in the playground

  • Breaks, your own or computed, are in the units of the rate, and the legend shows the rate. Without a label:, the legend heading names it (cases per 100,000 population).
  • A tract whose population is missing, 0 or not a number is no data, as is one whose count is missing. No feature is ever coloured by Infinity or NaN.
  • normalize: works for colour and for radius:; it needs numeric classes (not categories:), and multiply: needs normalize:.

A feature has no data when its value is null or missing, an empty string, or text that is not a number ("n/a"). Numeric strings such as "15", which CSV-derived GeoJSON is full of, are read as numbers.

classify:
field: median_income
breaks: [25000, 50000, 75000]
noData: "#eeeeee" # a colour (default: light grey #cccccc), or hide

noData: hide leaves those features off the map instead.

By default classify drives the layer type’s main colour: fill-color, line-color, circle-color, fill-extrusion-color, or text-color on a symbol layer. property: picks another colour property:

classify:
field: risk
breaks: [1, 2]
palette: OrRd
property: circle-stroke-color
Layer typeproperty:
fillfill-color, fill-outline-color
lineline-color
circlecircle-color, circle-stroke-color
fill-extrusionfill-extrusion-color
symboltext-color, icon-color, text-halo-color, icon-halo-color

One owner per property: writing both classify: and paint.fill-color on the same layer is an error (keep one). Other paint properties, such as fill-opacity or circle-stroke-width, sit beside classify as usual. A hand-written step, match or interpolate in paint: keeps working on any layer, beside classified ones (Layers & styling).

A map shows a legend by itself when a layer is classified or carries its own legend: entry. It sits top-left, open, under a “Legend” header the reader can click to fold it.

A classified layer lists a heading (the layer’s label:, else the field name), one line per class with its real edges, and a “No data” line:

  • < 25,000, 25,000–50,000, ≥ 75,000 for explicit breaks;
  • 3–10 … 31–55, using the data’s lowest and highest value, when the data is GeoJSON (so the edges are the dataset’s own);
  • “Other” and “No data” only when they can occur (with GeoJSON data, only when some feature actually has no data).

Edges follow the reader’s language. format: in the classify block takes the same number options as a popup (Format numbers): format: { currency: USD, max-fraction-digits: 0 } gives $25,000–$50,000.

Any other layer joins the legend with an entry of its own:

- id: parks
type: fill
source: parks
paint:
fill-color: "#22c55e"
legend:
label: Parks
color: "#22c55e"
shape: square # square (default), circle, line or icon

A legend: block at the top level of the document places and titles it:

legend:
title: Land use # a heading inside the panel
position: bottom-left # top-left (default), top-right, bottom-left, bottom-right
collapsed: true # start folded
  • legend: false at the top level means no legend; legend: false on a layer leaves that layer (and its classes) out.
  • items: in the block lists entries you write yourself (color, label, shape), instead of the layers’ own entries. A classified layer’s classes still follow them. When those items describe colours from before you added classify:, remove them; validation points this out.
  • A label: also gives the layer a checkbox in the map’s layer panel; add toggleable: false to keep the heading without the checkbox.
  • The header reads “Legend” in every map, so a screen reader always announces it the same way. Translate it, and the “No data” and “Other” lines, with maplibre-yaml.Legend.Title, maplibre-yaml.Classify.NoData and maplibre-yaml.Classify.Other in config.locale (Accessibility & languages).

Every legend key is in the map reference. A story map pins its legend by the same rules, with the same classes.

An exported style.json (mlym emit) carries the compiled expression, so the map draws the same without maplibre-yaml. Computed breaks are worked out at export: an https:// data URL is fetched, a path is read from disk (relative to the YAML file; a /… path from the nearest public/ folder). If the data can’t be loaded, the export fails and names the layer and the URL. Palette names are written out as colours, and the layer’s metadata records the field, method, breaks and colours.

The expression for the income layer with breaks [25000, 50000, 75000]
"fill-color": [
"let", "value", ["get", "median_income"],
["case",
["any",
["all", ["==", ["typeof", ["var", "value"]], "number"], ["==", ["var", "value"], ["var", "value"]]],
["all", ["==", ["typeof", ["var", "value"]], "string"], ["!=", ["var", "value"], ""],
["==", ["to-number", ["var", "value"], 0], ["to-number", ["var", "value"], 1]]]],
["step", ["to-number", ["var", "value"]], "#eff3ff", 25000, "#bdd7e7", 50000, "#6baed6", 75000, "#2171b5"],
"#cccccc"]
]

The case is the no-data guard (a number that is not NaN, or a non-empty numeric string); inside it is the plain step. Categories compile to a match behind the same kind of guard, and a rate divides inside the guard.

Validation catches most slips at the line they happen on, with a “did you mean” where it can. The common ones:

  • Every feature is “No data”. The field name is usually wrong: property names are case-sensitive (Median_Income is not median_income), and numeric classes need a numeric property. Once the data has loaded, the console (and, while you develop, a note on the map) names the field and the data’s actual property names. The same goes for normalize:.
  • “method: computes breaks from the whole dataset, and …”: the source is tiled, or a FlatGeobuf read by view. Write breaks: instead; mlym breaks computes them from the whole file (above).
  • Breaks that don’t go up, or a colors: (or radius:) list whose length doesn’t match the classes: n breaks make n + 1 classes.
  • Too few colours: the palette doesn’t go up to that many classes; lower classes: or pick another palette.
  • classify: and paint.fill-color both written: keep one.
  • radius: on a layer that isn’t a circle layer, normalize: with categories:, or multiply: without normalize:.

In format v2, classify: sits on the layer under style.layers[] (it is style), with label: under the layer’s runtime::

version: 2
type: map
id: income-v2
style:
basemap: "https://tiles.openfreemap.org/styles/positron"
center: [-75.19, 39.936]
zoom: 12
sources:
tracts: { type: geojson, url: /data/classify/sample-tracts.geojson }
layers:
- id: income
type: fill
source: tracts
classify: { field: median_income, method: quantile, palette: Blues }
runtime: { label: Median household income }
runtime:
legend: { position: bottom-left }

The list of validation checks is now under Troubleshooting.