When to use a choropleth
A choropleth map colors geographic regions based on a value — the shape of the region is fixed (it's a real border), and only the fill color changes to encode your metric:
- Covid cases by country
- Sales by state
- Population density
It's the right chart whenever "where" is part of the story and the reader would recognize the region shapes (countries, states) — if the regions aren't recognizable, a plain bar chart sorted by value usually communicates the ranking better.
Example dataset (country metrics)
import pandas as pd
df = pd.DataFrame({
"country": ["India", "United States", "Brazil"],
"iso_alpha": ["IND", "USA", "BRA"],
"value": [120, 200, 150],
})
print(df)Plotly Express choropleth
Plotly works best with ISO-3 codes (iso_alpha) — a standard 3-letter country code (IND, USA, BRA) that Plotly matches against its built-in map geometry, instead of trying to guess borders from a country name string.
import plotly.express as px
fig = px.choropleth(
df,
locations="iso_alpha",
color="value",
hover_name="country",
color_continuous_scale="Viridis",
title="Metric by country",
)
fig.show()color_continuous_scale picks the gradient used to map value to a color — Plotly interpolates between the scale's colors for every value in between the min and max.
Tips
- Prefer ISO codes for reliable mapping — country/state names have spelling and alias mismatches (e.g. "USA" vs "United States") that silently drop rows from the map.
- Consider log scale for heavy-tailed metrics — a few huge values otherwise wash out all the smaller ones into the same pale color.
- Always add a clear title and color legend so a reader can translate color back into a number.
flowchart LR A["DataFrame:
region id + value"] --> B["locations= column
(e.g. iso_alpha)"] B --> C["Plotly matches id
to built-in map geometry"] C --> D["color=value
mapped through color_continuous_scale"] D --> E["Shaded map,
hover shows exact value"]
Check your understanding
What changes between regions in a choropleth?
Region shapes are fixed real borders and only the fill color encodes your metric.
Show answer
B — The fill color that encodes the metric — Region shapes are fixed real borders and only the fill color encodes your metric.
Why does the lesson prefer iso_alpha codes such as IND, USA and BRA over country names?
Names such as USA versus United States can fail to match the map geometry. ISO-3 codes are standard.
Show answer
B — Names have spelling and alias mismatches that silently drop rows — Names such as USA versus United States can fail to match the map geometry. ISO-3 codes are standard.
What does color_continuous_scale="Viridis" set?
It picks the gradient and Plotly interpolates colors between the minimum and maximum values.
Show answer
B — The gradient mapping values to colors — It picks the gradient and Plotly interpolates colors between the minimum and maximum values.
A few huge values make every other country the same pale color. What does the lesson suggest?
A log scale handles heavy-tailed metrics so smaller values are still distinguishable.
Show answer
C — Consider a log scale — A log scale handles heavy-tailed metrics so smaller values are still distinguishable.
Next
Continue to: Interactive Histograms and Distributions for exploring how values are spread out.
🧪 Try It Yourself
Exercise 1 – Match Regions with locations
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 – Pick a Color Scale
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 – Show Full Country Names on Hover
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.