To create a heatmap, we need dataset in the form of XYZ. Where X and Y are the usual long/lat locations and Z can be any value/parameter you want the heatmap to represent (in this can our Z is temperature values).
Here I got this dataset as seen below:-
First we have to read the data and prepare it into a format that the folium heatmap plugin wanted.
The expected format is a list of list... that is: [ [x1, y1, z1], [x2, y2, z2], [x3, y3, z3] ... ]. This is easy to prepare with the pandas module.
import pandas as pd
import folium
from folium import plugins
from folium.plugins import HeatMap
# Load the data in df...
heatmap_df = pd.read_excel(r"Folium Heatmap Data.xlsx")
# heatmap_df.head()
# Convert the df individual rows into list
df_list = heatmap_df.values.tolist()
df_list
Now, that the data is ready we need to plot the heatmap as follow;-
lon, lat = -105.028306, 39.725343
m = folium.Map([lat, lon], zoom_start=10)
HeatMap(df_list, min_opacity=0.1).add_to(folium.FeatureGroup(name='Heat Map').add_to(m))
folium.LayerControl().add_to(m)
display(m)
The example below use random arrays generated from numpy library and the tile basemap is set to 'stamentoner' as seen below.
import numpy as np
import folium
from folium import plugins
from folium.plugins import HeatMap
lon, lat = -86.276, 30.935
zoom_start = 5
data = (
np.random.normal(size=(100, 3)) *
np.array([[1, 1, 1]]) +
np.array([[48, 5, 1]])
).tolist()
m = folium.Map([48, 5], tiles='stamentoner', zoom_start=6)
HeatMap(data, min_opacity=0.1).add_to(folium.FeatureGroup(name='Heat Map').add_to(m))
folium.LayerControl().add_to(m)
display(m)
That is it!
Reference resources
- https://github.com/python-visualization/folium
- https://nbviewer.jupyter.org/github/python-visualization/folium/blob/master/examples/TilesExample.ipynb
- https://leaflet-extras.github.io/leaflet-providers/preview/
- https://nbviewer.jupyter.org/github/python-visualization/folium_contrib/tree/master/notebooks/
No comments:
Post a Comment