Image by Author

COVID-19 vaccinations — An animated Choropleth Map with Plotly Express

How to create a map of US States, animated over time, showing the rollout of the Pfizer COVID-19 Vaccine with less than 20 lines of code.

Michael Taverner
Published in
3 min readApr 4, 2021

--

A couple of caveats to get out of the way: This case study looks only at the Pfizer vaccine, and the population data used is from 2019. The data is used for the purpose of demonstrating the use of the Plotly Express Choropleth map only.

Plotly express is a fantastic, high-level and user-friendly way to interface with Plotly. It abstracts away so much of the complexity involved in creating many of the beautiful plots available, and below we’ll focus on one of my favorite — the animated Choropleth Map.

Imports:

import pandas as pd
import plotly.express as px

Data:

The data for this exercise was obtained from:

There was a little bit of data wrangling and feature engineering involved to get the data into the state I used for the animation. I’ve provided the wrangled data in this Gist, and you can see the wrangling operations in the GitHub repo for this project.

I chose to use the cumulative sum of first-dose allocations per state on a per-capita basis to show the percentage of coverage for the populations of each state.

Animated Choropleth Map

px.choropleth(data_frame = data.query(‘jurisdiction != “Alaska”’),
locations=’state_codes’,
color=’vax_per_cap’,
template = ‘plotly_dark’,
color_continuous_scale=”viridis”,
animation_frame=’week’,
locationmode=’USA-states’,
basemap_visible=False,
scope=’usa’,
height=600,
width=800,
title=’Pfizer COVID-16 Vaccine allocations’,
hover_name = ‘jurisdiction’,
labels={‘vax_per_cap’:’Vaxes % of Population’,
‘week’:’Week’,
‘state_codes’:’State Code’})

Above you can see the resulting map, and a short demonstration of some of the built-in interactivity provided by Plotly Express.

Explanation of the arguments:

data_frame : Takes a DataFrame or dictionary as input, in this case I filtered out Alaska purely because it was way ahead of the pack in terms doses per capita, and messed with the colour scale :)

locations : Plotly express needs the State Codes (not state names) for a US map, which was helped along by a dictionary provided by rogerallen and is included in the GitHub repo. Cheers Rog!

color : this is the column or series that is going to tell the Plotly object how to colour the map.

template : Gives you an easy way to access some default colouring. You can chose from ['ggplot2', 'seaborn', 'simple_white', 'plotly', 'plotly_white', 'plotly_dark' etc.

color_continuous_scale : Depending on the way the data provided to the ‘colour’ column is distributed, it can be useful to choose different colour scales.

animation_frame: This is the column that is going to dictate how the colour will change from frame-to-frame, and is usually a time-series data point.

basemap_visible : just gets rid of any land areas that don’t have corresponding data — Alaska in this case.

labels: A dictionary I used to clean up some of the data labels in the legend and hover tool-tips.

A word on colour scaling: You may notice the colour scale bar on the right is dynamic, ie. changing over the course of the animation based on the min and max values at each frame. There’s an argument called range_color which takes an upper and lower limit as a tuple and allows you to lock in a colour range. I didn’t use it here, because I wanted to show the comparison between states through time, but it can give you control if you want the variable passed to colorto be compared to a set upper and lower bound.

Conclusion!

That’s all for now. Obviously the data here isn’t a perfect or an ultra-reliable source of COVID-19 Vaccine rollout data, but hopefully it’s a decent demonstration of the super powerful and easy to use Plotly Express, and somewhat hopeful indication of where the world may be headed in these troubling times. Please, please feel free to fork the repo and add in more vaccine manufacturers and make other adjustments as you like! Would love to see what you come up with in the comments.

--

--

Michael Taverner
Analytics Vidhya

I'm an Australian Fraud Prevention Data Analyst, live sound engineer and data science enthusiast.