Choropleth maps using Plotly in Python

A choropleth map is a map that displays data on geographic regions using different colors or shades to represent values. The data is usually represented as a color scale, with darker colors indicating higher values and lighter colors indicating lower values. Choropleth maps are useful for visualizing data on a map, such as demographic data, election results, or economic indicators.

Plotly is a Python data visualization library that allows users to create interactive plots and charts. It offers a range of features for creating custom visualizations, including line charts, scatterplots, bar charts, and choropleth maps. In this tutorial, we will learn how to create choropleth maps using Plotly Express.

Installing Plotly

Before we can start creating choropleth maps using Plotly, we need to install the library ?

pip install plotly

Loading Sample Data

We will use the Gapminder dataset, which contains data on life expectancy, GDP per capita, and population for different countries over time ?

import pandas as pd
import plotly.express as px

# Load the Gapminder dataset
df = px.data.gapminder()
print(df.head())
       country continent  year  lifeExp       pop   gdpPercap iso_alpha iso_num
0  Afghanistan      Asia  1952   28.801   8425333  779.445314       AFG       4
1  Afghanistan      Asia  1957   30.332   9240934  820.853030       AFG       4
2  Afghanistan      Asia  1962   31.997  10267083  853.100710       AFG       4
3  Afghanistan      Asia  1967   34.020  11537966  836.197138       AFG       4
4  Afghanistan      Asia  1972   36.088  13079460  739.981106       AFG       4

The dataset contains data for each country from 1952 to 2007, including ISO country codes needed for mapping.

Basic Choropleth Map

Let's create a basic choropleth map showing GDP per capita with animation over years ?

import plotly.express as px

df = px.data.gapminder()

fig = px.choropleth(
    df, 
    locations="iso_alpha",
    color="gdpPercap",
    hover_name="country",
    animation_frame="year",
    projection="natural earth"
)

fig.show()

Parameters Explained

  • locations Column containing ISO 3166-1 alpha-3 country codes
  • color Column for color mapping (GDP per capita)
  • hover_name Column for country names on hover
  • animation_frame Column for animation (year)
  • projection Map projection type

Customizing Color Scale

You can change the default color scale to better suit your data visualization needs ?

import plotly.express as px

df = px.data.gapminder()

fig = px.choropleth(
    df, 
    locations="iso_alpha",
    color="gdpPercap", 
    hover_name="country",
    animation_frame="year", 
    projection="natural earth",
    color_continuous_scale=px.colors.sequential.Greens
)

fig.show()

Adding Color Bar and Midpoint

Customize the color bar with a title and set a midpoint for better data representation ?

import plotly.express as px

df = px.data.gapminder()

fig = px.choropleth(
    df, 
    locations="iso_alpha",
    color="gdpPercap",
    hover_name="country",
    animation_frame="year",
    projection="natural earth",
    color_continuous_scale=px.colors.sequential.Greens,
    color_continuous_midpoint=10000
)

fig.update_layout(coloraxis_colorbar=dict(title="GDP per Capita"))
fig.show()

The color_continuous_midpoint parameter sets the midpoint of the color scale, while update_layout() customizes the color bar title.

Adding Title and Labels

Create a complete visualization with proper title and labels ?

import plotly.express as px

df = px.data.gapminder()

fig = px.choropleth(
    df, 
    locations="iso_alpha",
    color="gdpPercap",
    hover_name="country",
    animation_frame="year",
    projection="natural earth",
    color_continuous_scale=px.colors.sequential.Greens,
    color_continuous_midpoint=10000,
    title="GDP per Capita by Country (1952-2007)",
    labels={"gdpPercap": "GDP per Capita", "year": "Year"}
)

fig.update_layout(title_x=0.5)
fig.show()

The title parameter sets the map title, labels customizes axis labels, and title_x=0.5 centers the title.

Static Choropleth for Specific Year

Create a non-animated map for a specific year ?

import plotly.express as px

df = px.data.gapminder()
df_2007 = df[df['year'] == 2007]

fig = px.choropleth(
    df_2007, 
    locations="iso_alpha",
    color="gdpPercap",
    hover_name="country",
    projection="natural earth",
    title="GDP per Capita by Country (2007)",
    color_continuous_scale="Viridis"
)

fig.show()

Conclusion

Plotly makes creating choropleth maps straightforward with px.choropleth(). You can customize colors, add animations, and enhance maps with titles and labels. These maps are excellent for visualizing geographic data patterns and trends over time.

Updated on: 2026-03-27T16:43:20+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements