Generate an interactive choropleth India map using Corona Virus(COVID-19) pandemic data with Bokeh
By: Prince Nihith
Tags: Choropleth Map, Coronavirus, Matplotlib, Python, Bokeh

12 easy steps in this article helps you making an interactive Choropleth map using pandemic data(India) of Corona Virus(COVID-19) in a simplified detailed way. Make sure that you have gone through the previous article on Static Choropleth Maps

Bokeh

It is an intuitive perception library for present day internet browsers. It gives exquisite, brief development of flexible designs, and bears elite intelligence over huge or gushing datasets. Bokeh can help any individual who might want to rapidly and effectively make intelligent plots, dashboards, and information applications.

12 easy steps to interactive Choropleth Map :

Step-1 : Import all the libraries required for the code

# Import libraries

import pandas as pd

import numpy as np

import geopandas as gpd

import json from bokeh.io import show

from bokeh.plotting import figure

from bokeh.models import GeoJSONDataSource, LinearColorMapper, ColorBar

Step-2 : Read the excel data using pandas dataframe

data_df_india = pd.read_excel(r’data_ecxel.xlsx’,dtype=object)

data_df_india.rename(columns={‘Total Confirmed cases’:’Cases’,’Name of State/ UT’:’st_nm’},inplace=True)

data_df_india.head()

Read the excel data using the pandas dataframe and rename the columns for our convenience.

Step-3 : Read the shape file using Geopandas library

fp = r’india-polygon.shp’

sf_india = gpd.read_file(fp)

sf_india.head()

Step-4 : Merge the data file and shape file on the common column

merged = sf_india.merge(data_df_india, on = ‘st_nm’, how = ‘left’)

merged.head()

Here’s the common column st_nm

Step-5 : Read the data into JSON and convert into GEOJSON format

#Read data to json

merged_json = json.loads(merged.to_json()) #Convert to str like object

json_data = json.dumps(merged_json)

geosource = GeoJSONDataSource(geojson = json_data)

Step-6 : Configuring the palette

Palette — In the context of Bokeh, a palette is a simple plain Python list of (hex) RGB color strings.

For example the Blues8 palette which looks like is defined as:

palette = brewer[‘YlGnBu’][8]

#Reverse color order so that dark blue is highest obesity.

palette = palette[::-1]

Step-7 : Instantiating the Color Mapper and tick labels

color_mapper = LinearColorMapper(palette = palette, low=merged[‘Cases’].min(), high = merged[‘Cases’].max(), nan_color = ‘#d9d9d9’)

#Define custom tick labels for color bar.

tick_labels = {‘5000’: ‘>5000’}

We have low and high values depending upon the column ‘Cases’ in Merged dataframe. We have assigning the color value as ‘#d9d9d9’ for Nan In column Merge.

Step-8: Add hover tools to make the map more interactive

Hover Tool — The hover tool is a passive inspector tool. It is generally on at all times, but can be configured in the inspector’s menu associated with the toolbar.

#Add hover tool

hover = HoverTool(tooltips = [ (‘Country’,’@st_nm’),(‘Cases’,’@Cases’)])

We are adding the column st_nm for state name and column Cases for Confirmed cases to display the data of particular pointed state.

Step-9 : Add the Color Bar map

#Create color bar.

color_bar = ColorBar(color_mapper=color_mapper, label_standoff=8,width = 500, height = 20,

border_line_color=None,location = (0,0), orientation = ‘horizontal’, major_label_overrides = tick_labels)

Step-10 : Now create the figure object

p = figure(title = ‘Number of Covid-19 cases’, plot_height = 600 , plot_width = 950, toolbar_location = None, tools = [hover])

p.xgrid.grid_line_color = None

p.ygrid.grid_line_color = None

Step-11: Add patches to the figure

#Add patch renderer to figure.

p.patches(‘xs’,’ys’, source = geosource,fill_color = {‘field’ :’Cases’, ‘transform’ : color_mapper}, line_color = ‘black’, line_width = 0.25, fill_alpha = 1)

#Specify layout

p.add_layout(color_bar, ‘right’)

Step-12: Display the plot using show method

show(p)

Github link:

You can get the code and files here