Visualization - Folium HeapMap with Time

Folium HeatMapWithTime Plugin

In this example we show the most basic usage of the HeatMapWithTime plugin.

We generate a random set of points with lat/lon coordinates to draw on the map, and then move these points slowly in a random direction to simulate a time dimension. The points are aranged into a list of sets of data to draw.

import folium
import folium.plugins as plugins
import numpy as np

np.random.seed(3141592)
initial_data = (
    np.random.normal(size=(100, 2)) * np.array([[1, 1]]) +
    np.array([[48, 5]])
)

move_data = np.random.normal(size=(100, 2)) * 0.01

data = [(initial_data + move_data * i).tolist() for i in range(1000)]

weight = 1  # default value
for time_entry in data:
    for row in time_entry:
        row.append(weight)
m = folium.Map([48., 5.], tiles='stamentoner', zoom_start=5)

hm = plugins.HeatMapWithTime(data, auto_play=True, min_speed=10.0)

hm.add_to(m)

m

Now we show that the time index can be specified, allowing a more meaningful representation of what the time steps mean. We also enable the ‘auto_play’ option and change the maximum opacity. See the docmentation for a full list of options that can be used.

from datetime import datetime, timedelta


time_index = [
    (datetime.now() + k * timedelta(1)).strftime('%Y-%m-%d') for
    k in range(len(data))
]
m = folium.Map([48., 5.], tiles='stamentoner', zoom_start=6)

hm = plugins.HeatMapWithTime(
    data,
    index=time_index,
    auto_play=True,
    max_opacity=0.3
)

hm.add_to(m)

m