[GIS] Using Folium HeatMapWithTime Plugin

foliumheat mappandaspython

I am using pandas, and folium to replicate the following example, but with my CSV data to produce a heat map.

Here's what my data looks like:

data

and here is the code I am using currently:

import pandas as pd
import numpy as np
import folium.plugins as plugins  
from datetime import datetime, timedelta


    data = [(df3).tolist() for i in range(100)]

    m = folium.Map([40., -73.], tiles='stamentoner', zoom_start=6)

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

    hm.add_to(m)
    hm.save('map.html')

The example works, but I cannot input my own data, as it results in errors. Would anyone know how to plot the X, and Y data from my CSV or an approach to get me started to produce a heatmap?

Best Answer

You can start importing your csv data (file_csv) in panda in a dataframe (df) with a code like :

import pandas as pd

df = pd.read_csv(file_csv) 

(see réf. https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html).

To convert a pandas dataframe (df) to a numpy ndarray, use this code:

df=df.values 

df now becomes a numpy ndarray.

For the time, there's some documentation in the heat_map_with_time.py script. I have to look further (Ref. https://github.com/python-visualization/folium/blob/master/folium/plugins/heat_map_withtime.py)

Form the class infos:

index: Index giving the label (or timestamp) of the elements of data. Should have the same length as data, or is replaced by a simple count if not specified.

From what i understand, you need to have a time index dataframe the same size as the data dataframe (or array)