Exploring Choropleth Maps

Learn how to create choropleth maps and utilize animation frames using Plotly Express.

Choropleth maps

Choropleth maps are basically colored polygons representing a certain area on a map. Plotly ships with country maps included (as well as US states), and so it is very easy to plot maps if we have information about countries. We already have such information in our dataset. We have country names, as well as country codes, in every row. We also have the year, some metadata about the countries (region, income group, and so on), and all the indicator data. In other words, every data point is connected to a geographical location. So, let’s start by choosing a year and an indicator and see how the values of our chosen indicator vary across countries.

Press + to interact
import pandas as pd
poverty = pd.read_csv('data/poverty.csv')
year = 2016
indicator = 'GINI index (World Bank estimate)'
df = poverty[poverty['is_country'] & poverty['year'].eq(year)]
import plotly.express as px
px.choropleth(df, locations="Country Code", color=indicator)
  • Lines 2–4: We open the poverty file into a DataFrame and create the year and indicator variables.

  • Line 6: We create a subset of poverty with values from the selected year and containing countries only.

  • Line 8: We create a choropleth map using the choropleth function from Plotly Express, by choosing the column that identifies the countries and the column that will be used for the colors.

We can see the result of the preceding code in the Jupyter Notebook set up below:

Please login to launch live app!

The country codes we provided were already included in Plotly and are in the three-letter ISO format. As with scatterplots, we can see that since we provided a numeric column for the color, a continuous color scale was chosen. Otherwise, we would have gotten a discrete color sequence. For example, setting color='Income Group' would produce the chart in the Jupyter Notebook set up below:

Please login to launch live app!

Using normal country names

We can also use normal country names to plot them. To do that, we only need to set locationmode='country names' ...