Skip to content

Instantly share code, notes, and snippets.

@amankharwal
Created November 27, 2020 06:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amankharwal/150b5c610d67f57167926c26af89afab to your computer and use it in GitHub Desktop.
Save amankharwal/150b5c610d67f57167926c26af89afab to your computer and use it in GitHub Desktop.
import geopandas as gpd
import numpy as np
import pandas as pd
import datetime as dt
import matplotlib.pyplot as plt
import plotly.express as px
import plotly.figure_factory as ff
def covid_plot(df, abs = True, Today=True, date=None):
if df != 'deaths' and df != 'confirmed_cases':
print('available option for df are: "deaths" or "confirmed_cases"')
if df == 'deaths' or df == 'confirmed_cases':
initial_data=dt.datetime(2020,1,23)
us = pd.read_csv('covid19-data-from-john-hopkins-university/CONVENIENT_us_{}.csv'.format(df),low_memory=False).iloc[1:,1:]
us = np.sum(us.astype(float),axis=1)
us = us.rename('United States of America')
data = dt.date.today().strftime("%d/%m/%Y")
death = pd.read_csv('covid19-data-from-john-hopkins-university/CONVENIENT_global_{}.csv'.format(df))
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
df_new=pd.DataFrame()
states=['Australia','Canada','China','Denmark','France','Netherlands','United Kindom']
for i in states:
colonna = ([col for col in death.columns if i in col])
df_new = pd.concat([df_new,(np.sum(death[colonna].iloc[1:].astype(float),axis=1))],axis=1)
death = death.drop(columns=colonna)
df_new.columns = states
death = death.drop(columns=['Country/Region'])
death = pd.concat([death,df_new],axis=1)
death = death.T
death = death.drop(columns=[0])
death = death.append(us)
state = death.index
death.index = range(0,len(death))
death['name']= state
if Today==True and date==None:
test = pd.merge(world,death.iloc[:,-2:],on=['name'],how='left')
test.iloc[:,-1] = test.iloc[:,-1].apply(lambda x: float(x))
if abs==False:
population = test.iloc[:,0]
population_mil=population//1000000
test.iloc[:,-1] = test.iloc[:,-1] //population_mil
if Today==True and date!=None:
print('Choose only a date!')
if Today==False:
if date==None:
print('Either specifying a date or select Today = True to set date for today.')
if date!=None:
if type(date) != dt.datetime:
print('be sure that date format is: "dt.datetime(YYYY,M,DD)"')
if type(date) == dt.datetime:
data = date
index=((data-initial_data).days)
if index < 0:
print('data are recorded starting from 2020,1,23; be sure to select a value after this date')
if index >= 0:
data = (data.strftime("%d/%m/%Y"))
test = pd.merge(world,death[[index,'name']],on=['name'],how='left')
test.iloc[:,-1] = test.iloc[:,-1].apply(lambda x: float(x))
if abs==False:
population = test.iloc[:,0]
population_mil=population//1000000
test.iloc[:,-1] = test.iloc[:,-1] //population_mil
if df == 'confirmed_cases':
maxs=200000
if abs==False:
maxs=1000
if df == 'deaths':
maxs=3500
if abs==False:
maxs=35
ab = 'Absolute'
if abs==False:
ab= 'Relative'
fig,ax=plt.subplots(figsize=(25,25))
test.plot(column=test.iloc[:,-1],vmin=0,vmax=maxs,legend=True,
legend_kwds={
'label': "number of {}".format(df),
'orientation': "horizontal"},
missing_kwds={
'color':'lightgrey',
'label':'Missing values'
} , ax=ax
)
ax.set_title("{} world covid-19 {} in {}".format(ab,df,data), fontsize=25)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment