Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add heatmap to a layer in Folium

I have this sample code:

from glob import glob
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).add_to(m)
m

enter image description here

How can I add this heat map to a layer so I can hide it if needed?

like image 621
Luis Ramon Ramirez Rodriguez Avatar asked Feb 18 '19 17:02

Luis Ramon Ramirez Rodriguez


People also ask

How to make a heat map with folium?

The secret for the heat map was simply using a plugin from folium passing a matrix with the latitude and longitude of each station and a radius. Finally, before closing out, I’d like to highlight another feature of folium, map tiles.

How to create a map of volcanoes in folium?

To represent the different types of volcanoes, you can create Folium markers and add them to your map. Make this Notebook Trusted to load map: File -> Trust Notebook Folium is well known for its heatmaps, which create a heatmap layer. To plot a heatmap in Folium, you need a list of latitudes and longitudes.

How can I select or deselect particular object on folium map?

1 Add menu bar on folium map to select or deselect particular object (Marker) Related 3124 How can I add new keys to a dictionary? 189 Plotting a 2D heatmap with Matplotlib

How do I use folium?

If you're new into Folium, I strongly advise to read this introduction. It explains the basics: install the library, initialize a map, change tile, save as standalone html file, embed the map somewhere and so on.


1 Answers

I would first add your HeatMap to a FeatureGroup and then add that FeatureGroup to the map(m). I would then add a LayerControl to your map (check the upper right corner). Does this suffice?

from glob import glob
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).add_to(folium.FeatureGroup(name='Heat Map').add_to(m))
folium.LayerControl().add_to(m)

m

enter image description here

like image 60
Bob Haffner Avatar answered Sep 29 '22 17:09

Bob Haffner