Skip to content

Instantly share code, notes, and snippets.

@felipenunezb
Forked from sergiolucero/covid_folium.py
Last active March 12, 2020 11:01
Show Gist options
  • Save felipenunezb/5ac57a0fa58a2b97d9360e0a18a5951e to your computer and use it in GitHub Desktop.
Save felipenunezb/5ac57a0fa58a2b97d9360e0a18a5951e to your computer and use it in GitHub Desktop.
plotting COVID advance
import folium, pandas as pd
from folium.plugins import MarkerCluster
pdf = pd.read_json('https://tinyurl.com/covid19-github')
pdf = pdf[pdf.data==pdf.data.max()]
pdf = pdf[pdf.totale_casi>0]
location = pdf.describe()[['lat','long']].loc['50%'].values
fm = folium.Map(location=location, zoom_start=6, tile='stamentoner',
width=800, height=600)
mc = MarkerCluster()
for row in pdf.itertuples():
mc.add_child(folium.Marker(location=[row.lat, row.long],
popup=row.denominazione_provincia))
mc.add_to(fm)
fm
import folium, pandas as pd, numpy as np
from folium.plugins import HeatMapWithTime
pdf = pd.read_json('https://tinyurl.com/covid19-github')
pdf = pdf[pdf.totale_casi>0]
pdf = pdf[(pdf.lat>0) & (pdf.long > 0)]
# Calculate weight relative to max number of cases per location
max_prov = pdf[['lat','long','totale_casi']].groupby(['lat','long']).max().rename(columns={'totale_casi': 'Max_Provincia'})
pdf = pd.merge(pdf, max_prov, on=['lat','long'], how='left')
pdf['weight'] = round(pdf['totale_casi'] / pdf['Max_Provincia'], 5) #Weight based on highest value (should decrease as people recover)
# format data for heatmapwithtime
final_df = []
for dt in np.unique(pdf['data']):
final_df.append(pdf[pdf['data']==dt][['lat','long','weight']].values.tolist())
#location = pdf.describe()[['lat','long']].loc['50%'].values
location = [41.54, 12.29]
fm = folium.Map(location=location, zoom_start=5, width=640, height=480)
HeatMapWithTime(data=final_df, index=np.unique(pdf['data']).tolist()).add_to(fm)
fm
@felipenunezb
Copy link
Author

covid_heatmapwithtime

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment