Animated choropleth map with Plotly: Covid-19 use case
Create GIF out of geographical data
I’ve been looking at the news about Covid-19 but I was not entirely satisfied by the way they were presenting the data. I wanted to plot the Covid-19 data by myself. I started doing data viz with Pandas, Seaborn, and the next step was geographical data. I want to get the evolution of confirmed Covid-19 cases overtime per country.
I actually spent hours going through online tutorials looking for the easiest way to make maps (specifically choropleths). I eventually found Plotly to be THE one.
Plotly has great documentation and is pretty easy to use. For geographical plotting, it supports two kinds of data GeoJSON and country names (as defined in Natural Earth dataset). In the data I’ll be using, the countries name is already a feature of the dataset. So I went for that option.
Here what we get:
I’m writing this article using Covid-19 data from John Hopkins University but any piece of this code is made to be easily reproducible (using high-level functions, generic names…). The full code is accessible in this notebook.
The prerequisites of this code, are the following:
!pip install pycountry-convert!pip install chart-studio
Getting the data
I’ll work on the daily report of confirmed cases of Covid-19 per country. This dataset starts on 22nd January 2020 and is updated every day by the JHU team. The daily updated folder is present here.
urls = {'confirmed':'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv'}
Cleaning and prepping the data
Goal: month over month evolution of the confirmed cases of Covi-19
I wrapped up the cleaning steps in one function where I format the date as %Y/%m/%d and only keep the count of Covid cases at the end of each month.
We can now easily create a dataframe out of this function and here what we get:
Working with Plotly, we have to use the ISO code of the country. We can use pycountry library to add this information to the dataset :
By looking at the data, we can see that some countries are duplicated due to their different province (e.g. United Kingdom). To make the mapping more consistent, we sum the number of Covid cases by country.
Here what the final data is looking like:
We are ready to map!
Plotting the data
Saving images directly through Plotly can be a bit tricky. The easiest way to proceed is to go through chart_studio. So here are the libraries we will need:
I wrapped the plotting steps in a function as generic as possible:
It takes as an input a dataset and a point in time. Here I chose the month but if you’d rather plot the evolution per day, it’s seamlessly customizable.
Plotly choropleth function main arguments are:
- locations: we set which column to look at to get the countries (here it’s the ISO code)
- color: indicates which features the choropleth scale will be plotted against
- hover_name: it’s optional, indicates which title to display when mousing over a specific part (country or region) or the map
- hover_date: it’s optional, you can choose what data to display in addition to the previous name
- range_color: the color scale of the choropleth map
- color_continous_scale: color palette and scale (can be continuous or logarithmic)
Warning: to use chart_studio you need to have a username and an API key. To get so, log in to chart_studio, sign in/up, and get your key.
We can now make a plot for each month :
Making a GIF
We have maps showing change over time: this is the perfect use case to make a GIF. Making a GIF is about looping over the images produced :
- duration: set the time spent over each image
- loop: if 0 infinite loop
- append_images: list of images to append to the first one (the order has to be consistent)
Tada!
Et voila! You’re ready to play with any choropleth with Plotly and turn them into sharable images of GIF. You can access the full code on Github.