In [3]:
##Install and import all the required packages in Pythonpackages for the Python Project.
import geoplot as gplt
import geopandas as gpd
In [4]:
import geoplot.crs as gcrs
import pandas as pd
import pathlib
import matplotlib.pyplot as plt
import mapclassify as mc
import numpy as np
import imageio
In [6]:
%matplotlib inline
In [7]:
# Read spatial data as shapefile- Karnataka Districts
Kar = gpd.read_file("Spatialdata/Karnataka_dist_final.shp")
Kar.head()
Out[7]:
KeyID OBJECTID Shape_Leng Shape_Area Dist_numbe Dist_Name geometry
0 1 0 7.209818e+09 7.209818e+09 17 Chikkamagaluru POLYGON ((646368.928 1501649.279, 646419.356 1...
1 1 0 4.251288e+09 4.251288e+09 29 Chikkaballapura POLYGON ((845727.186 1501783.909, 846421.234 1...
2 1 0 5.639345e+09 5.639345e+09 27 Chamarajanagara POLYGON ((752250.808 1361335.689, 752225.480 1...
3 1 0 5.453491e+09 5.453491e+09 5 Bidar MULTIPOLYGON (((767717.940 1968293.755, 767760...
4 1 0 2.195914e+09 2.195914e+09 20 Bengaluru (Urban) MULTIPOLYGON (((777128.955 1415825.254, 777114...
In [8]:
# Read CSV File COVID19 dataset(9th March to 20th July, 2020)
covid_19 = pd.read_csv("9March_20July_Covid19_Karnataka.csv")
covid_19.head()
Out[8]:
Dist_Name 9/03/2020 10/03/2020 11/03/2020 12/03/2020 13/03/2020 14/03/2020 15/03/2020 16/03/2020 17/03/2020 ... 11/07/2020 12/07/2020 13/07/2020 14/07/2020 15/07/2020 16/07/2020 17/07/2020 18/07/2020 19/07/2020 20/07/2020
0 Bagalkot 0 0 0 0 0 0 0 0 0 ... 233 210 214 288 319 346 374 357 394 445
1 Ballari 0 0 0 0 0 0 0 0 0 ... 605 648 722 799 874 869 965 1095 1080 1276
2 Belagavi 0 0 0 0 0 0 0 0 0 ... 97 96 121 185 226 300 391 498 583 643
3 Bengaluru (Rural) 0 0 0 0 0 0 0 0 0 ... 192 311 332 346 356 387 420 514 584 792
4 Bengaluru (Urban) 1 1 1 1 0 0 1 4 3 ... 12793 14067 15052 15599 17051 18828 20623 22449 24316 25574

5 rows × 135 columns

In [9]:
# To know the data type of csv reading through pandas
print(type(covid_19)) 
<class 'pandas.core.frame.DataFrame'>
In [10]:
#Transpose of the csv file, Where Date will be rows and district names will be columns

covid_19_transposed = covid_19.T

covid_19_transposed.head()
Out[10]:
0 1 2 3 4 5 6 7 8 9 ... 20 21 22 23 24 25 26 27 28 29
Dist_Name Bagalkot Ballari Belagavi Bengaluru (Rural) Bengaluru (Urban) Bidar Chamarajanagara Chikkaballapura Chikkamagaluru Chitradurga ... Mandya Mysuru Raichur Ramanagara Shivamogga Tumakuru Udupi Uttara Kannada Vijayapura Yadgir
9/03/2020 0 0 0 0 1 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
10/03/2020 0 0 0 0 1 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
11/03/2020 0 0 0 0 1 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
12/03/2020 0 0 0 0 1 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0

5 rows × 30 columns

In [11]:
# Check is there any mismatch in District Name in both the files

for items in covid_19['Dist_Name'].to_list():
    Districts = Kar['Dist_Name'].to_list()
    
    if items in Districts:
        pass
    else:
        print(items +' is not in list')
In [12]:
# Merge operation for Covid_19 csv file and shapefile

Kar_covid19 = Kar.merge(covid_19, left_on="Dist_Name", right_on="Dist_Name")
Kar_covid19.head()
Out[12]:
KeyID OBJECTID Shape_Leng Shape_Area Dist_numbe Dist_Name geometry 9/03/2020 10/03/2020 11/03/2020 ... 11/07/2020 12/07/2020 13/07/2020 14/07/2020 15/07/2020 16/07/2020 17/07/2020 18/07/2020 19/07/2020 20/07/2020
0 1 0 7.209818e+09 7.209818e+09 17 Chikkamagaluru POLYGON ((646368.928 1501649.279, 646419.356 1... 0 0 0 ... 45 53 54 54 58 66 89 111 145 169
1 1 0 4.251288e+09 4.251288e+09 29 Chikkaballapura POLYGON ((845727.186 1501783.909, 846421.234 1... 0 0 0 ... 140 175 217 230 225 289 319 425 522 582
2 1 0 5.639345e+09 5.639345e+09 27 Chamarajanagara POLYGON ((752250.808 1361335.689, 752225.480 1... 0 0 0 ... 109 121 102 92 83 93 84 118 104 140
3 1 0 5.453491e+09 5.453491e+09 5 Bidar MULTIPOLYGON (((767717.940 1968293.755, 767760... 0 0 0 ... 332 373 396 438 418 466 491 562 585 530
4 1 0 2.195914e+09 2.195914e+09 20 Bengaluru (Urban) MULTIPOLYGON (((777128.955 1415825.254, 777114... 1 1 1 ... 12793 14067 15052 15599 17051 18828 20623 22449 24316 25574

5 rows × 141 columns

In [29]:
# Saving updated shapefile as new shapefile in project folder
Kar_covid19.to_file('C:/Users/rahis/Desktop/Project/Kar_covid.shp')
In [13]:
# Basic plot,
Kar_covid19.plot()
Out[13]:
<AxesSubplot:>
In [14]:
# Read updated shapefile to view its attribute table 
Kar_covid= gpd.read_file("Kar_covid.shp")
Kar_covid.head()
Out[14]:
KeyID OBJECTID Shape_Leng Shape_Area Dist_numbe Dist_Name 9/03/2020 10/03/2020 11/03/2020 12/03/2020 ... 12/07/2020 13/07/2020 14/07/2020 15/07/2020 16/07/2020 17/07/2020 18/07/2020 19/07/2020 20/07/2020 geometry
0 1 0 7.209818e+09 7.209818e+09 17 Chikkamagaluru 0 0 0 0 ... 53 54 54 58 66 89 111 145 169 POLYGON ((646368.928 1501649.279, 646419.356 1...
1 1 0 4.251288e+09 4.251288e+09 29 Chikkaballapura 0 0 0 0 ... 175 217 230 225 289 319 425 522 582 POLYGON ((845727.186 1501783.909, 846421.234 1...
2 1 0 5.639345e+09 5.639345e+09 27 Chamarajanagara 0 0 0 0 ... 121 102 92 83 93 84 118 104 140 POLYGON ((752250.808 1361335.689, 752225.480 1...
3 1 0 5.453491e+09 5.453491e+09 5 Bidar 0 0 0 0 ... 373 396 438 418 466 491 562 585 530 MULTIPOLYGON (((767717.940 1968293.755, 767760...
4 1 0 2.195914e+09 2.195914e+09 20 Bengaluru (Urban) 1 1 1 1 ... 14067 15052 15599 17051 18828 20623 22449 24316 25574 MULTIPOLYGON (((777128.955 1415825.254, 777114...

5 rows × 141 columns

In [15]:
# Individual district plot
Kar_covid19[Kar_covid19.Dist_Name =="Bidar"].plot()
Out[15]:
<AxesSubplot:>
In [16]:
# to know the datatype of a shapefile
print(type(Kar_covid)) 
<class 'geopandas.geodataframe.GeoDataFrame'>
In [17]:
# Plot by Dist_name, Random colors
Kar_covid.plot(column='Dist_Name')
Out[17]:
<AxesSubplot:>
In [18]:
## Standard Map layout with legend

ax = Kar_covid.plot(column = Kar_covid.Dist_Name,
            cmap = 'OrRd',
            figsize =(25,14),
            legend = True,
            #legend_kwds ={'label': "COVID-19 cases", 'orientation' : "vertical"},
            edgecolor = 'black')
            #linewidth = 0.8) 
In [19]:
#Plot Dist name with an accurate legend
fig, ax = plt.subplots(1, 1)

Kar_covid.plot(column='Dist_Name', ax=ax, legend=True)
Out[19]:
<AxesSubplot:>
In [20]:
#Plot Dist name with an accurate legend
fig, ax = plt.subplots(1, 1)

Kar_covid.plot(column='9/03/2020', ax=ax, legend=True)
Out[20]:
<AxesSubplot:>
In [21]:
# Plot Covid active cases per day with an accurate legend

from mpl_toolkits.axes_grid1 import make_axes_locatable
In [22]:
fig, ax = plt.subplots(1, 1)

divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.2)
Kar_covid.plot(column='20/07/2020', ax=ax, legend=True,  
               legend_kwds={'label': "COVID 19 by Districs",
                           'orientation': "vertical"}, cax=cax)
Out[22]:
<AxesSubplot:>
In [23]:
# Horizontal legend 
fig, ax = plt.subplots(1, 1)

Kar_covid.plot(column='20/07/2020', ax=ax,
               legend=True,
               legend_kwds={'label': "COVID 19 by Districs",
                           'orientation': "horizontal"})
Out[23]:
<AxesSubplot:>
In [24]:
#Choosing colors using cmap
Kar_covid.plot(column='Dist_Name', cmap='OrRd')
Out[24]:
<AxesSubplot:>
In [25]:
 #Boundary file
Kar_covid.boundary.plot()
Out[25]:
<AxesSubplot:>
In [26]:
Kar_covid.plot(column='20/07/2020', cmap='OrRd', scheme='quantiles')
Out[26]:
<AxesSubplot:>
In [27]:
#  Resize figure and adjusting Map layout the axes
ax = Kar_covid.plot(column =('9/03/2020'),
                 cmap = 'Reds',
                 figsize =(40,14),
                 legend = True,
                 edgecolor = 'black',
                 linewidth = 0.5)
ax.set_title ('Total Active COVID19 Cases in Karnataka : '+ ('9/03/2020'), fontdict= 
                 {'fontsize': 15}, pad = 12.5)
Out[27]:
Text(0.5, 1.0, 'Total Active COVID19 Cases in Karnataka : 9/03/2020')
In [28]:
ax = Kar_covid.plot(column =('20/07/2020'),
                 cmap = 'Reds',
                 figsize =(40,14),
                 legend = True,
                 edgecolor = 'black',
                 linewidth = 0.5)
ax.set_title ('Total Active COVID19 Cases in Karnataka : '+ ('20/07/2020'), fontdict= 
                 {'fontsize': 15}, pad = 12.5)

#Removing the axes

ax.set_axis_off()
In [29]:
import io
import PIL
In [30]:
# Dates in the form of LIST
for dates in Kar_covid.columns.to_list()[6:139]:
    print(dates)
9/03/2020
10/03/2020
11/03/2020
12/03/2020
13/03/2020
14/03/2020
15/03/2020
16/03/2020
17/03/2020
18/03/2020
19/03/2020
20/03/2020
21/03/2020
22/03/2020
23/03/2020
24/03/2020
25/03/2020
26/03/2020
27/03/2020
28/03/2020
29/03/2020
30/03/2020
31/03/2020
1/04/2020
2/04/2020
3/04/2020
4/04/2020
5/04/2020
6/04/2020
7/04/2020
8/04/2020
9/04/2020
10/04/2020
11/04/2020
12/04/2020
13/04/2020
14/04/2020
15/04/2020
16/04/2020
17/04/2020
18/04/2020
19/04/2020
20/04/2020
21/04/2020
22/04/2020
23/04/2020
24/04/2020
25/04/2020
26/04/2020
27/04/2020
28/04/2020
29/04/2020
30/04/2020
1/05/2020
2/05/2020
3/05/2020
4/05/2020
5/05/2020
6/05/2020
7/05/2020
8/05/2020
9/05/2020
10/05/2020
11/05/2020
12/05/2020
13/05/2020
14/05/2020
15/05/2020
16/05/2020
17/05/2020
18/05/2020
19/05/2020
20/05/2020
21/05/2020
22/05/2020
23/05/2020
24/05/2020
25/05/2020
26/05/2020
27/05/2020
28/05/2020
29/05/2020
30/05/2020
31/05/2020
1/06/2020
2/06/2020
3/06/2020
4/06/2020
5/06/2020
6/06/2020
7/06/2020
8/06/2020
9/06/2020
10/06/2020
11/06/2020
12/06/2020
13/06/2020
14/06/2020
15/06/2020
16/06/2020
17/06/2020
18/06/2020
19/06/2020
20/06/2020
21/06/2020
22/06/2020
23/06/2020
24/06/2020
25/06/2020
26/06/2020
27/06/2020
28/06/2020
29/06/2020
30/06/2020
1/07/2020
2/07/2020
3/07/2020
4/07/2020
5/07/2020
6/07/2020
7/07/2020
8/07/2020
9/07/2020
10/07/2020
11/07/2020
12/07/2020
13/07/2020
14/07/2020
15/07/2020
16/07/2020
17/07/2020
18/07/2020
19/07/2020
In [31]:
# Creating GIF file
image_frames = []

for dates in Kar_covid.columns.to_list()[6:10]:
    ax = Kar_covid.plot(column = dates,
                 cmap = 'OrRd',
                 figsize =(14,14),
                 legend = True,
                 legend_kwds ={'label' : "COVID-19 Active cases", 'orientation' : "vertical"},
                 edgecolor = 'black',
                 linewidth = 0.8)     
    ax.set_title ('Total Active COVID19 Cases in Karnataka : '+ dates, fontdict= {'fontsize': 22}, pad = 12.5)
    ax.set_axis_off()