8

Create COVID-19 Map Animation with Python in 5 Minutes

 2 years ago
source link: https://towardsdatascience.com/covid-19-map-animation-with-python-in-5-minutes-2d6246c32e54
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

DATA VISUALIZATION

Create COVID-19 Map Animation with Python in 5 Minutes

Using Python with Plotly to Create a COVID-19 Map Animation

0*bnXFctWPp50508cj?q=20
covid-19-map-animation-with-python-in-5-minutes-2d6246c32e54
Photo by Matt Lamers on Unsplash

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:

1*b_tSPjWNfArnI3LPmVuSAQ.png?q=20
covid-19-map-animation-with-python-in-5-minutes-2d6246c32e54
The COVID-19 Dashboard by CSSE at JHU (Screenshot on May 29, 2020)

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.

Let’s get started!

Step 1: Prepare Python Libraries

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

Step 2: Load Dataset

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.csvdataset in this article.

time_series_covid19_confirmed_global.csv from JHU CSSE

Then, you can easily load the data to dataframe with

import pandas as pd
df_confirm =
pd.read_csv('path_to_file')
1*YsD96jqjdnoOchDOOUL2wQ.png?q=20
covid-19-map-animation-with-python-in-5-minutes-2d6246c32e54
time_series_covid19_confirmed_global Dataset

Step 3: Clean Dataset

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:

1*nAnff3CH5wsg3SCwwQ9akw.png?q=20
covid-19-map-animation-with-python-in-5-minutes-2d6246c32e54
Cleaned time_series_covid19_confirmed_global Dataset

Step 4: Create Map Animation with Plotly Express

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.

Python script to create animated choropleth from time_series_covid19_confirmed_global Dataset with Plotly Express
1*JsDG7AyfQk2SHdLYKfIqag.gif?q=20
covid-19-map-animation-with-python-in-5-minutes-2d6246c32e54
Animated choropleth from time_series_covid19_confirmed_global Dataset with Plotly Express

That’s about it! Easy right?

What’s Next?

This example visualizes the confirmed COVID-19 case dataset, but you may try yourself 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 visualization dynamic data through this article.

Conclusion

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. Feel free to left messages here or talk with me on LinkedIn if you have any comments, questions.

Be Safe and Healthy! Thank you for Reading. 👋😄

Find more of my articles here:

0*hlOCQ_D9XoyZJvXR.png?q=20
covid-19-map-animation-with-python-in-5-minutes-2d6246c32e54
https://medium.com/@JoeTS

References

[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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK