How to animate Scatterplots on Mapbox using Plotly Express? | by Usama Bin Tariq | Towards Data Science

Plotly Express is a data visualization library that allows us to visualize small bits of data quickly and efficiently. In this tutorial, we will be learning how to make animations using Plotly express’s built-in scatter Mapbox functionality.
The Scatter Mapbox requires the following arguments:
df, which is your Panda’s data frame.lat is the name of the column containing latitude coordinates.lon is the name of a column containing longitude coordinates.color is the name of the column for coloring individual scatters.size is the name of the column determining the size of the individual scatter.
df, which is your Panda’s data frame.
lat is the name of the column containing latitude coordinates.
lon is the name of a column containing longitude coordinates.
color is the name of the column for coloring individual scatters.
size is the name of the column determining the size of the individual scatter.
Though the above-mentioned arguments are basics and we can create a scatter plot through them, this is not all. Plotly Express Scatter Mapbox function offers a ton of functionalities and can be found at its documentation here.
With these understood we can now create a simple scatter plot on Mapbox which can be converted into an animation later on. But before this, we have to take a look and understand our dataset.
For this example, we will be using COVID-19 Dataset, that can be found here. It is further processed. For more information on the preprocessing of data, please check it here.
As shown in the dataset below, we have the first column that shows the name of the country, the next two columns show the latitude and longitude coordinates of the country. The Continent column shows the continent of the respective country. The date column shows the date when data for the country was last updated. The confirmed column represents confirmed COVID cases, deaths represent the number of people who have died due to COVID, and the Recovery column represents the number of people who are cured of the virus.
All the data shown for a specific country represents total accumulative numbers since the first case was recorded and not daily increase.
With the dataset understood, its time to create a scatter plot now. We will be creating a scatter plot for Confirmed COVID cases and plotting it on the Mapbox. Let’s see how we can do it.
Now since we are to visualize confirmed cases, the “confirmed” column would be forwarded to size and color arguments. Please take a look at the code below:
# Necessary Importingimport pandas as pdimport plotly.express as px#Importing dataset as pandas dataframedatasets = pd.read_csv('covid-dataset.csv')# Creating and visualizing a scatter plot on Mapboxfig = px.scatter_mapbox(datasets, lat="Lat", lon="Long",                  color="Confirmed", size="Confirmed",                  color_continuous_scale=px.colors.cyclical.IceFire,                  size_max=70, zoom=0.75, hover_name='Country',                   hover_data = ['Confirmed', 'Deaths', 'Recovery'],                   title = 'Accumulative COVID-19 Confirmed Cases                  till 17 June, 2020')fig.show()
As we can see from the code above, there are few unknown arguments here. Let’s break it one by one.
“datasets” is just the name of the panda’s data frame passed into the function.“lat” and “long” are names on columns inside a data frame with latitude and longitude coordinates.Since we are interested in visualizing confirmed cases, we have forwarded the “confirmed” column to size and color arguments.“color_continuous_scale” is asking for the color scheme we would be using for visualizing our data. There are few options available, please refer to the documentation link above.“size_max” is asking for the maximum size of the bubble plotted on the map. While “zoom” is the default zoom value to visualize your map. Feel free to play with both of these values and optimize as you like.“hover_name” and “hover_data” are asking for columns to be displayed when a cursor hovers over a specific bubble on the map.“title” as the name implies assigns the title of the map.
“datasets” is just the name of the panda’s data frame passed into the function.
“lat” and “long” are names on columns inside a data frame with latitude and longitude coordinates.
Since we are interested in visualizing confirmed cases, we have forwarded the “confirmed” column to size and color arguments.
“color_continuous_scale” is asking for the color scheme we would be using for visualizing our data. There are few options available, please refer to the documentation link above.
“size_max” is asking for the maximum size of the bubble plotted on the map. While “zoom” is the default zoom value to visualize your map. Feel free to play with both of these values and optimize as you like.
“hover_name” and “hover_data” are asking for columns to be displayed when a cursor hovers over a specific bubble on the map.
“title” as the name implies assigns the title of the map.
With this now understood, let’s visualize the output of the code snippet above.
Wow, that looks great. You can generate similar maps for deaths and recovery columns as well. Try it for yourself.
With that done, the next task is to animate the “date” column in the data frame and see how confirmed cases buildup since the outbreak of the virus in January.
The data set used previously was sliced to visualize data at the maximum recorded date, which was 17th June 2020. To create an animation we need to remove the slicer and understand our dataset once again. Let’s take a look.
The dataset contains recorded confirmed cases, deaths, and recovery from Coronavirus from 22nd January 2020 to 17th June 2020. All the rest columns in the database are the same as explained previously.
Now, we need to animate date on a Scatter Mapbox plot and visualize how COVID19 confirmed cases have spread around the world. To this, we need to give a few extra arguments to the scatter_mapbox function.
fig = px.scatter_mapbox(datas, lat="Lat", lon="Long",            animation_frame = 'Date', animation_group = 'Country',             color="Confirmed", size="Confirmed",            color_continuous_scale=px.colors.cyclical.IceFire,             size_max=70, zoom=0.75, hover_name='Country',             hover_data = ['Confirmed', 'Deaths', 'Recovery'],             title = 'Visualizing spread of COVID from 22/1/2020 to            17/6/2020')fig.show()
As evident from the code above, two additional arguments are given to the scatter_mapbox function to create an animation. These arguments are “animation_frame” and “animation_group”. “animation_frame” asks for a column containing time-series information that is to be animated. With this being done, we have finally generated the animation below:
That’s it for this tutorial, please take a look at GitHub for this project, here.