##Install and import all the required packages in Pythonpackages for the Python Project.
import geoplot as gplt
import geopandas as gpd
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
%matplotlib inline
# Read spatial data as shapefile- Karnataka Districts
Kar = gpd.read_file("Spatialdata/Karnataka_dist_final.shp")
Kar.head()
# Read CSV File COVID19 dataset(9th March to 20th July, 2020)
covid_19 = pd.read_csv("9March_20July_Covid19_Karnataka.csv")
covid_19.head()
# To know the data type of csv reading through pandas
print(type(covid_19))
#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()
# 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')
# 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()
# Saving updated shapefile as new shapefile in project folder
Kar_covid19.to_file('C:/Users/rahis/Desktop/Project/Kar_covid.shp')
# Basic plot,
Kar_covid19.plot()
# Read updated shapefile to view its attribute table
Kar_covid= gpd.read_file("Kar_covid.shp")
Kar_covid.head()
# Individual district plot
Kar_covid19[Kar_covid19.Dist_Name =="Bidar"].plot()
# to know the datatype of a shapefile
print(type(Kar_covid))
# Plot by Dist_name, Random colors
Kar_covid.plot(column='Dist_Name')
## 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)
#Plot Dist name with an accurate legend
fig, ax = plt.subplots(1, 1)
Kar_covid.plot(column='Dist_Name', ax=ax, legend=True)
#Plot Dist name with an accurate legend
fig, ax = plt.subplots(1, 1)
Kar_covid.plot(column='9/03/2020', ax=ax, legend=True)
# Plot Covid active cases per day with an accurate legend
from mpl_toolkits.axes_grid1 import make_axes_locatable
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)
# 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"})
#Choosing colors using cmap
Kar_covid.plot(column='Dist_Name', cmap='OrRd')
#Boundary file
Kar_covid.boundary.plot()
Kar_covid.plot(column='20/07/2020', cmap='OrRd', scheme='quantiles')
# 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)
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()
import io
import PIL
# Dates in the form of LIST
for dates in Kar_covid.columns.to_list()[6:139]:
print(dates)
# 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()