#!/usr/bin/env python # coding: utf-8 # 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]: get_ipython().run_line_magic('matplotlib', 'inline') # In[7]: # Read spatial data as shapefile- Karnataka Districts Kar = gpd.read_file("Spatialdata/Karnataka_dist_final.shp") Kar.head() # 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() # In[9]: # To know the data type of csv reading through pandas print(type(covid_19)) # 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() # 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() # 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() # In[14]: # Read updated shapefile to view its attribute table Kar_covid= gpd.read_file("Kar_covid.shp") Kar_covid.head() # In[15]: # Individual district plot Kar_covid19[Kar_covid19.Dist_Name =="Bidar"].plot() # In[16]: # to know the datatype of a shapefile print(type(Kar_covid)) # In[17]: # Plot by Dist_name, Random colors Kar_covid.plot(column='Dist_Name') # 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) # 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) # 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) # 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"}) # In[24]: #Choosing colors using cmap Kar_covid.plot(column='Dist_Name', cmap='OrRd') # In[25]: #Boundary file Kar_covid.boundary.plot() # In[26]: Kar_covid.plot(column='20/07/2020', cmap='OrRd', scheme='quantiles') # 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) # 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) # 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() # In[38]: # Adding all the images into an empty list and creating a GIF file as Dynamic Map image_frames = [] for dates in Kar_covid.columns.to_list()[6:139]: 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() img = ax.get_figure() f = io.BytesIO() img.savefig(f, format = 'png', bbox_inches = 'tight') f.seek(0) image_frames.append(PIL.Image.open(f)) image_frames[0].save('Dynamic COVID19 Map Karnataka.gif', format = 'GIF', append_images = image_frames[1:], save_all = True, duration = 300, loop = 3) f.close()