将热图添加到 Folium 中的图层

Add heatmap to a layer in Folium

我有这个示例代码:

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

如何将此热图添加到图层以便在需要时隐藏它?

我会先将您的 HeatMap 添加到一个 FeatureGroup,然后将该 FeatureGroup 添加到 map(m)。然后我会在您的地图中添加一个 LayerControl(检查右上角)。这够了吗?

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