vlspyros
vlspyros

Reputation: 1

Folium Heatmap With Time for COVID 19

I am trying to create a Heatmap movie for the confirmed cases of Covid 19.

My dataset is a pd.dataFrame with columns Date, Latitude, Longitude, Confirmed.

My issue is that I do not know how to input the Confirmed value as an input in the Folium.plugin.HeatmapWithTime.

I tried using:

new_map = folium.Map(location=[0, 0], tiles= "cartodbpositron",min_zoom=2, zoom_start=2, max_zoom=3)

df['Lat'] = df['Lat'].astype(float)
df['Long'] = df['Long'].astype(float)

Confirmed_df = df[['Lat', 'Long','Confirmed']]

hm = plugins.HeatMapWithTime(Confirmed_df,auto_play=True,max_opacity=0.8)
hm.add_to(new_map)

new_map

df looks like:

Date                     LAT           LONG    Confirmed 
2020/04/26             48.847306     2.433284   6500
2020/04/26             48.861935     2.441292   4800      
2020/04/26             48.839644     2.655109   9000   
2020/04/25             48.924351     2.386369   12000      
2020/04/25             48.829872     2.376677   0  

Upvotes: 0

Views: 3842

Answers (1)

strnam
strnam

Reputation: 1646

You should pre-process data before input to HeatMapWithTime() function. The Folium document and example here are helpful.

In your case, the input should be a list of [lat, lng, weight], you should use Confirmed column as a weight. The first thing, you need normalize 'Confirmed' values to (0, 1].

df['Confirmed'] = df['Confirmed'] / df['Confirmed'].sum()

Then, you can preprocess like this:

df['Date'] = df['Date'].sort_values(ascending=True)
data = []
for _, d in df.groupby('Date'):
   data.append([[row['lat'], row['lng'], row['Confirmed']] for _, row in d.iterrows()])

Finally, use data to input to function HeatMapWithTime() as you did:

hm = plugins.HeatMapWithTime(data, auto_play=True,max_opacity=0.8)
hm.add_to(new_map)
new_map

Upvotes: 4

Related Questions