Back To Top

Aditi Saha

Python to visualize COVID-19 projections

Using Python and some graphing libraries, you can project the total number of confirmed cases of COVID-19, and also display the total number of deaths for a country (this article uses India as an example) on a given date. Humans sometimes need help interpreting and processing the meaning of data, so this article also demonstrates how to create an animated horizontal bar graph for five countries, showing the variation of cases by date.

Projecting confirmed cases and deaths for India

This is done in three steps.

1. Download data

Scientific data isn’t always open, but fortunately, many modern science and healthcare organizations are eager to share information with each other and the public. Data about COVID-19 cases is available online, and it’s updated frequently.

To parse the data, you first must download it:

Load the data directly into a Pandas DataFrame. Pandas provides a function, read_csv(), which can take a URL and give back a DataFrame object.

The top row of the data set contains column names:

  1. Date
  2. Country
  3. Confirmed
  4. Recovered
  5. Deaths

The output of the head query includes a unique identifier (not listed as a column) plus an entry for each column.

The output of the tail query is similar but contains the tail end of the data set.

From the output, you can see that the DataFrame (df1) has the following columns:

  1. Date
  2. Country
  3. Confirmed
  4. Recovered
  5. Dead

2. Select data for India

In this step, we will select only those rows in the DataFrame that include India. This is shown in the script below:

3. Plot data

Here we create a bar chart. We will put the dates on the X-axis and the number of confirmed cases and the number of deaths on the Y-axis. There are a few noteworthy things about this part of the script which are as follows:

  • The line of code: plt.rcParams[“figure.figsize”]=20,20 is meant only for Jupyter. So remove it if you are using some other IDE.
  • Notice the line of code: ax1 = plt.gca(). To ensure that both the plots i.e. for confirmed cases as well as for deaths are plotted on the same graph, we need to give to the second graph the ax object of the plot. So we use gca() to do this. (By the way, ‘gca’ stands for ‘get current axis’).

Creating an animated horizontal bar graph for five countries

To run this in Jupyter as a dynamic animation rather than as a static png, you need to add a magic command at the beginning of your cell, namely: %matplotlib notebook. This will keep the figure alive instead of displaying a static png file and can hence also show animations. If you are on another IDE, remove this line.

1. Download the data

This step is exactly the same as in the previous script, and therefore, it need not be repeated.

2. Create a list of all dates

If you examine the data you downloaded, you notice that it has a column Date. Now, this column has a date value for each country. So the same date is occurring a number of times. We need to create a list of dates with only unique values. This will be used on the X-axis of our bar charts. We have a line of code like: list_dates = df[‘Date’].unique(). The unique() method will pick up only the unique values for each date.

3. Pick five countries and create an ax object

Take a list of five countries. (You can choose whatever countries you prefer, or even increase or decrease the number of countries). I have also taken a list of five colors for the bars of each country. (You can change this too if you like). One important line of code here is: fig, ax = plt.subplots(figsize=(15, 8)). This is needed to create an ax object.

4. Write the call back function

If you want to do animation in Matplotlib, you need to create an object of a class called matplotlib.animation.FuncAnimation. The signature of this class is available online. The constructor of this class, apart from other parameters, also takes a parameter called func, and you have to give this parameter a callback function. So in this step, we will write the callback function, which is repeatedly called in order to render the animation.

5. Create FuncAnimation object

This step has partly been explained in the previous step.

The three important parameters to be given are:

  • fig, which must be given a fig object, which we created earlier.
  • func, which must be the call back function.
  • frames, which must contain the variable on which the animation is to be done. Here in our case, it will be the list of dates we created earlier.

6. Save the animation to an mp4 file

You can save the animation created into an mp4 file. But for this you need ffmpeg. You can download this using pip by pip install ffmpeg-python, or using conda (on Jupyter) install -c conda-forge ffmpeg.

And finally, you can run the animation using Please note that on many platforms, the ffmpeg may not work properly and may require further “tweaking.”

error: Content is protected !!