简体   繁体   中英

Plotting a heat map with Folium

I am trying to plot a heat map with folium to show accident prone areas in the city but the map won't display.

First I generated the basemap with the code below which worked perfectly fine:

def generateBaseMap(default_location=[-1.286389,36.817223], default_zoom_start=12):
    base_map = folium.Map(location=default_location, control_scale=True, 
    zoom_start=default_zoom_start)
    return base_map
base_map = generateBaseMap()
base_map

Then I tried applying a heatmap to the basemap and that's where I'm having problems:

from folium import plugins
import folium.plugins as plugins
from folium.plugins import HeatMap
from folium.plugins import HeatMapWithTime

nrb1=HeatMap(data=df[['latitude','longitude']].groupby(['latitude','longitude']).sum().reset_index().values.tolist(), radius=7, max_zoom=10).add_to(base_map)

nrb1.save("Heatmap.png")
nrb1

All that I'm getting is: <folium.plugins.heat_map.HeatMap at 0x7fd607c04790> . No error message, no map displayed either. What am I doing wrong

You're trying to display the HeatMap object, but I think you need to display the Map

Try the following

# Create the HeatMap then add to base_map
HeatMap(data=df[['latitude','longitude']].groupby(['latitude','longitude']).sum().reset_index().values.tolist(), radius=7, max_zoom=10).add_to(base_map)

# display map
base_map

Thank you so much Bob, that worked perfectly well.

I have one more question, slightly unrelated, I have been using juplyterlab which by default opens with safari and lately when I plot the maps they start to flicker with a message that says "make this notebook trusted to load map: file ->trust notebook" but when I use colab on chrome, it works just fine. However, I prefer jupyterlab/jupyter notebook because the files don't get lost due to runtime and I can use it offline, is there sth I can do about this?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM