Visualize Coronavirus Cases Geographically in Python

Introduction 

 
In this article, we will visualize coronavirus (COVID-19) cases geographically using Python. 
 
Steps 
 
Install the chart_studio in your environment. Now Plotly is a part of chart_studio
  1. pip install chart_studio   
The data is picked from here. * I am using it for demo purposes.
 
Import all the necessary libraries and setup your jupyter notebook for offline plotly usage 
  1. import pandas as pd   
  2. import chart_studio.plotly as py  
  3. import plotly.graph_objs as go  
  4. from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot  
  5. init_notebook_mode(connected=True)  
  6.    
  7. # Lets get data from that url  
  8. df = pd.read_html('https://www.worldometers.info/coronavirus/#countries')  
  9.    
  10. # data is imported as a list  
  11. # use the indexing to get your dataframe  
  12. df = df[0]  
  13. type(df)  
  14. df.info()  
You will find lot of NA values. Let's clean up the data and also rename columns and change datatype
  1. df.rename(columns = {"Country,Other""COUNTRY"}, inplace=True)  
  2. # fill NaN and Change columntype  
  3. df["NewCases"] = df["NewCases"].fillna(0).astype('int')  
  4. df["TotalDeaths"] = df["TotalDeaths"].fillna(0).astype('int')  
  5. df["NewDeaths"] =df["NewDeaths"].fillna(0).astype('int')  
  6. df["TotalRecovered"] = df["TotalRecovered"].fillna(0).astype('int')  
  7. df["ActiveCases"] = df["ActiveCases"].fillna(0).astype('int')  
  8. df["Serious,Critical"] =df["Serious,Critical"].fillna(0).astype('int')  
  9. df.info()  
Now, we will create a new column text to get all this information as a string in single column so that when you hover over a country, it's easy to display.
  1. df['text'] = df.apply(lambda r : "Deaths: " + str(r.TotalDeaths + r.NewDeaths) + " Suspected: " + " " + str(r.NewCases + r.ActiveCases),  
  2. axis = 1)  
Now, when you see the head of this dataframe, you will get a data like below.
 
  COUNTRY TotalCases  NewCases  TotalDeaths  NewDeaths  ActiveCases  TotalRecovered  Serious,Critical  text 
0 China 79252 428 2835 47 37323 39094 7664 Deaths: 2882 Suspected: 37751
1 S. Korea 2931 594 17 1 2890 24 7 Deaths: 18 Suspected: 3484
2 Italy 889 0 21 0 822 46 64 Deaths: 21 Suspected: 822
3 Diamond Princess 705 0 6 0 689 10 36 Deaths: 6 Suspected: 689
4 Iran 388 0 34 0 281 73 0 Deaths: 34 Suspected: 281
 
Now lets replace the countries with their respective codes. This countrycode.csv is attached in the code zip file.
  1. country_code=pd.read_csv("countrycode.csv")  
  2. # Convert the dataframe to dictionary  
  3. country_code.set_index('COUNTRY', inplace=True)  
  4. dict_country_code = country_code.to_dict()  
  5. REPLACE_LIST = dict_country_code['CODE']   
  6.   
  7. # Replace Country with Codes  
  8. df.replace(REPLACE_LIST, inplace=True)  
Now we need to begin to build our data dictionary. The easiest way to do this is to use the dict() function of the general form:
  1.    type = 'choropleth',  
  2.    locations = country code  
  3.    colorscale= Either a predefined string:    'pairs' | 'Greys' | 'Greens' | 'Bluered' | 'Hot' | 'Picnic' | 'Portland' | 'Jet' | 'RdBu' | 'Blackbody'  
  4.                                                                            | 'Earth' | 'Electric' | 'YIOrRd' | 'YIGnBu'   
  5.                         or create a custom colorscale  
  6.    text= list or array of text to display per point  
  7.    z= array of values on z axis (color of state)  
  8.    colorbar = {'title':'Colorbar Title'})  
  9.    
  10. data = dict(  
  11.    type = 'choropleth',  
  12.    colorscale = 'ylorrd',  
  13.    locations = df['COUNTRY'].values,  
  14.    z = df['TotalCases'],  
  15.    text = df['text'],  
  16.    colorbar = {'title' : 'Corona Total Cases'},  
  17. )  
Then we create the layout nested dictionary and  Then we use
  1. go.Figure(data = [data],layout = layout)   
To set up the object that finally gets passed into iplot()
  1. layout = dict(  
  2.    title = 'Global Corona stats',  
  3.    geo = dict(  
  4.                showframe = False,  
  5.                projection = {'type':'natural earth'}  
  6.             )  
  7.    )  
  8.    
  9. choromap = go.Figure(data = [data],layout = layout)  
  10. iplot(choromap)  
You will see the geographical map as we aimed for. 
 
 
When you hover over each country, you will see data as in our text column.
 
Along with this, plotly has commands to hover, zoom in, zoom out, and save pictures.
 
 
From the data, only the Diamond Princess data is not shown.
 
The 'type' property is an enumeration that may be specified as ['equirectangular', 'mercator', 'orthographic', 'natural earth', 'kavrayskiy7', 'miller', 'robinson', 'eckert4', 'azimuthal equal area', 'azimuthal equidistant', 'conic equal area', 'conic conformal', 'conic equidistant', 'gnomonic', 'stereographic', 'mollweide', 'hammer', 'transverse mercator', 'albers usa', 'winkel tripel', 'aitoff', 'sinusoidal']
 
With any other data that is across the globe, you can create geographical charts. Plotly is just one of the ways, but it can be done in many other ways.


Recommended Free Ebook
Similar Articles