在 Folium HeatMapWithTime 中显示日期、id 列和其他列

Display date, id column and other columns in Folium HeatMapWithTime

我有这样的出租车频率数据集:

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

我正在尝试使用 folium 创建一个随时间变化的热图,以显示不同日期的出租车数量。我正在尝试执行以下操作:

  1. 如何在动画上显示'Date'作为索引?
  2. 如何在每个热图点上显示'ID'?
  3. 如何在每个热图点上显示'Sample_Mean'?

这是我试过的:

# 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)

folium 中的热图动画唯一可以做的就是将索引显示为日期。据我所知,无法显示或注释任何其他值。相反,您可以通过阈值和圆半径的大小来设置颜色区分。这是一个 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