Reputation: 439
I have a taxi frequency dataset like this:
ID Date Latitude Longitude Sample_Mean
01 2019-10-09 40.431753 -3.711294 0.1
03 2019-10-21 40.431753 -3.711294 0.05
32 2020-03-22 40.458772 -3.699815 0.2
44 2020-03-29 40.431753 -3.711294 0.22
I am trying to create a heat map with time using folium to display the volume of taxi's on different days. I'm trying to do the following:
This is what I have tried:
# Make basemap
map = folium.Map(location=[25.41, -3.703], zoom_start=15, tiles='CartoDB positron')
df['date'] = df['date'].sort_values(ascending=True)
data = []
for _, d in df.groupby('date'):
data.append([[row['latitude'], row['longitude'], row['Sample_Mean']] for _, row in d.iterrows()])
hm = plugins.HeatMapWithTime(data, auto_play=True,display_index=True,max_opacity=0.8)
hm.add_to(map)
Upvotes: 3
Views: 1231
Reputation: 35205
The only thing that can be done with the heatmap animation in folium is to display the index as a date. As far as I know, it is not possible to display or annotate any other value. Instead, you can set the color distinction by threshold and the size of the circle radius. Here is an example. See also the official sample.
import pandas as pd
import numpy as np
import io
data = '''
ID date latitude longitude Sample_Mean
01 2019-10-09 40.431753 -3.711294 0.1
03 2019-10-21 40.431753 -3.711294 0.2
04 2019-10-30 40.431753 -3.711294 0.8
32 2020-03-22 40.458772 -3.699815 0.4
44 2020-03-29 40.458772 -3.699815 0.66
45 2020-04-07 40.458772 -3.699815 0.95
'''
df = pd.read_csv(io.StringIO(data), delim_whitespace=True)
df['date'] = df['date'].sort_values(ascending=True)
data = []
for _, d in df.groupby('date'):
data.append([[row['latitude'], row['longitude'], row['Sample_Mean']] for _, row in d.iterrows()])
import folium
import folium.plugins as plugins
m = folium.Map(location=[40.43, -3.703], zoom_start=13, tiles='CartoDB positron')
hm = plugins.HeatMapWithTime(data, auto_play=True,
display_index=True,
gradient={0.2: 'blue', 0.4: 'lime', 0.6: 'orange', 1: 'red'},
index=df['date'].tolist(),
max_opacity=0.8)
hm.add_to(m)
m
Upvotes: 5