Generating Heatmap for Corona Virus Propagation in Python gmaps

Abrar Fahim
Mar 16, 2020 · 2 min read
Photo by Clay Banks on Unsplash

In this article, I will show how to plot heatmap with gmaps. First, we need to install gmaps using pip, from terminal or cmd you can type,

pip install gmaps

from jupyter notebook cell,

!pip install gmaps

We also need pandas to read CSV files and get the amazing functionalities of pandas dataframe. For installing pandas with pip,

pip install pandas

from jupyter notebook cell,

!pip install pandas

First, we need to import the dependencies,

import pandas as pd
import gmaps
from IPython.display import display

We collected dataset from this source, https://www.kaggle.com/grebublin/coronavirus-latlon-dataset

We will use “CV_LatLon_21Jan_12Mar.csv” file for visualization.

data = pd.read_csv(“CV_LatLon_21Jan_12Mar.csv”)
df = pd.DataFrame(data)
df.head()

Now, look carefully at the dataset. This dataset has some latitude and longitude where corona spread is not confirmed. Look at the column “confirmed”, it has many zero values. we have to delete these rows where corona confirmed is zero.

mylist = [“confirmed”]
df2 = df[df[mylist].ne(0).all(1)]
df2

.ne(0) means not equal to zero. So df2 has all the latitude and longitude where the corona is confirmed.

gmaps.configure(api_key=””)
locations = df2[[‘lat’, ‘lon’]]
weights = df2[‘confirmed’]
fig = gmaps.figure()
fig.add_layer(gmaps.heatmap_layer(locations, weights=weights, max_intensity=100, point_radius=20.0))
fig
Generating Heatmap with latitude and longitude data

Here, API key is not given as we do not have any API key for google maps. Location selected from the “lat” and “lan” values from the df2 data frame. We also give the weights “confrimed” for the heatmap.

The intensity is given as 100, and points_radius given as 20. You can change the value to see the changes in heatmap.

If in jupyter notebook the figure is not shown you have to open the terminal and run the following commands,


jupyter nbextension enable --py gmaps
jupyter nbextension enable --py widgetsnbextension

Now let me share the full code,

Conclusion

I hope you enjoyed the tutorial. Let me know if you have any suggestions on my post in the comments. Thank you for going through this lengthy post. I will be glad if you can share your thoughts with me on Linkedin .

Codephilics

We are a digital transformation consultancy and software…

Codephilics

We are a digital transformation consultancy and software development company that provides cutting edge engineering solutions. Our services are designed to take your business to the next level, reducing the gap between you and the heights of success you wish to achieve.

Abrar Fahim

Written by

Codephilics

We are a digital transformation consultancy and software development company that provides cutting edge engineering solutions. Our services are designed to take your business to the next level, reducing the gap between you and the heights of success you wish to achieve.