Module 8 · Plotly and maps · Lesson 40 of 66

Choropleth maps

Color states and countries by a number, with hover text and map projections.

In this lesson · about 18 minutes

  • Build a choropleth from data and layout with graph_objects
  • Map US states with locationmode='USA-states' and scope='usa'
  • Map the world by country code, and do it in one line with Plotly Express

A choropleth map colors each region (a state, a country) by a number, like population or income. In Plotly you describe it in two parts:

  • data: a go.Choropleth trace saying which places (locations), how to read them (locationmode), the number for each (z), the hover text, and the colorscale.
  • layout: how the map looks, such as its title and geo settings like scope='usa'.

Older tutorials write both as plain dictionaries, dict(type='choropleth', ...), and call iplot. go.Figure accepts the same dictionaries, so both styles work.

A first US map

Real data: US farm exports

The file 2011_US_AGRI_Exports.csv has one row per state with its two-letter code, its total exports in millions of dollars, and a ready-made text column for the hover label. marker_line draws white borders between the states.

Exercise

Map one product

Using the farm exports table df, map the beef column by state, with the 'Reds' color scale and the map limited to the USA.

Code that runs before yours
import pandas as pd
import plotly.graph_objects as go
df = pd.read_csv('2011_US_AGRI_Exports.csv')

World maps

For countries, give locations as three-letter ISO codes such as USA or ETH; that's the default locationmode. Pick how the globe is flattened with geo=dict(projection={'type': 'mercator'}). Other projections include 'natural earth', 'robinson' and 'orthographic' (a globe you can spin). 2014_World_GDP.csv has each country's GDP in billions of dollars.

The one-line version

Plotly Express builds the same map from column names: px.choropleth(df, locations='CODE', color='GDP (BILLIONS)', hover_name='COUNTRY'). For US states add locationmode='USA-states' and scope='usa'.

Exercise

World GDP with Plotly Express

Use px.choropleth to map GDP (BILLIONS) by country code (CODE), with the country name shown on hover and the 'orthographic' projection.

Code that runs before yours
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
df = pd.read_csv('2014_World_GDP.csv')

Exercises solved: 0 of 2

Tip: solve each exercise first. Press Check to test your code.

Noviq Academy's Data Science course is free and for learning. Python runs in your browser with Pyodide, which downloads about 20 MB the first time, plus a few more for scikit-learn. Your code and progress stay on this device. The salaries data is a sample of San Francisco's public pay records; the purchases, customers, housing, advertising and classified data are made up. The Titanic, tips, flights and iris data sets come from Seaborn's examples; the maps use public 2011 to 2014 US and world figures. The 911 data is a 20,000-call sample of Montgomery County, Pennsylvania's public 911 records; the movie ratings are the MovieLens 100K data from GroupLens Research; the reviews are a 3,000-review sample from the Yelp Dataset Challenge; the loans are public LendingClub data; the colleges are the College data from An Introduction to Statistical Learning; the bank notes, kyphosis and breast cancer data come from the UCI Machine Learning Repository. Spark and TensorFlow can't run in a browser, so those lessons use stand-ins that run here, and show the code for the real tools.