Create COVID-19 Map Animation with Python in 5 Minutes | by Joe T. Santhanavanich | Towards Data Science

In the fight against COVID-19, The GIS technologies have played an important role in many aspects, including the data integration, and geospatial visualization of epidemic information, spatial tracking of confirmed cases, prediction of regional transmission, and many more. These provide support information for government sectors to fight against the COVID-19 spreading. [1] To address this, JHU had provided such a nice dashboard created with ESRI ArcGIS operation dashboard:
The dashboard gives a pretty nice overview of the total cumulative confirmed cases, active cases, incidence rate, case-fatality ratio, testing rate, and hospitalization rate. However, the feature for visualizing the change of data overtime on the map is missing!
This article will introduce you to an easy way to create a dynamic map visualizing the spreading of COVID-19 overtime using the JHU COVID-19 dataset.
The Python libraries we will use in this article are mainly Pandas, PyCountry, and Plotly Express. Most of you would already know the Plotly library, but if not, you may check over all of its features in this article.
$ pip install pandas$ pip install pycountry$ pip install plotly
The dataset we will here is the JHU CSSE COVID-19 dataset. You can download or fork the latest update version of it from the CSSE Github repo. You may check this article to check how to automatically update the datasource over time in your Python project with PythonGit. We will focus only on the time_series_covid19_confirmed_global.csv dataset in this article.
Then, you can easily load the data to dataframe with
import pandas as pddf_confirm = pd.read_csv('path_to_file')
In order to visualize the time series dataset dynamically with Plotly Express, you need to have these requirements for your dataframe.
Aggregate the dataset into the level of the country.
Get the three-letter country codes defined in ISO 3166–1 for each country; for example, AFG for Afghanistan. You can do this by using PyCountry.
Transform the dataset in a long format in which date value is representing in a single column You can do this easily starting by loading the dataset into the Pandas dataframe and then using melt in Pandas.
You may follow these steps with this Python script:
Overall the final clean dataframe would look like in the following image:
After the dataset is ready, you can easily create a map animation with Plotly Express. For example, you can create an animated choropleth map from the dataframe from step 3 with the following script below, you may adjust each parameter as you like too.
This example visualizes the confirmed COVID-19 case dataset, but you may try to apply this method for visualizing the COVID-19 death case and recovered case dataset as well.
For more information about plotting the animation choropleth map with plotly, check the full documentation here. If you want to change the color scale, you can simply choose the built-in color scale from here. Apply better color symbology rule. Try to create other map types such as scatter maps or heat maps by checking from the Plotly map gallery here.
Alternatively, you may check how to visualize COVID-19 data in Kepler.gl a geospatial analysis tool for the visualization of dynamic data through this article.
This article introduces you to how to prepare datasets and create Map Animation with Plotly Express in an easy way. I hope you like this article and found it useful for your daily work or projects. Feel free to leave me a message if you have any questions or comments.
About me & Check out all my blog contents: Link
Be Safe and Healthy! 💪
Thank you for Reading. 📚
[1] Zhou, C., Su, F., Pei, T., Zhang, A., Du, Y., Luo, B., Cao, Z., Wang, J., Yuan, W., Zhu, Y., Song, C., Chen, J., Xu, J., Li, F., Ma, T., Jiang, L., Yan, F., Yi, J., Hu, Y., ... Xiao, H. (2020). COVID-19: Challenges to GIS with Big Data. Geography and Sustainability, 1(1), 77–87. https://doi.org/10.1016/j.geosus.2020.03.005