Python Central HubSign in

Data Analytics with Python / Phase 7: Interactive Visualization with Plotly

Choropleth Maps

Create geographical choropleth maps in Plotly to visualize metrics by country/region.

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)

Python
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.

Python
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.

sketch Choropleth = shape stays fixed, only fill color changes p5.js
Each cell is a stand-in 'region'; darker amber means a higher value, mimicking a continuous color scale.

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.
diagram Choropleth data flow mermaid
Your value column is matched to map geometry through a region identifier, then colored by a scale.

Check your understanding

quizCheck yourself
  1. What changes between regions in a choropleth?

    Show answer

    B — The fill color that encodes the metric — Region shapes are fixed real borders and only the fill color encodes your metric.

  2. Why does the lesson prefer iso_alpha codes such as IND, USA and BRA over country names?

    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.

  3. What does color_continuous_scale="Viridis" set?

    Show answer

    B — The gradient mapping values to colors — It picks the gradient and Plotly interpolates colors between the minimum and maximum values.

  4. A few huge values make every other country the same pale color. What does the lesson suggest?

    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

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Pick a Color Scale

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Show Full Country Names on Hover

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did