import pandas as gpd
import folium
from folium.plugins import HeatMap
# loading the shapefile and adding it to the folium map
shp = gpd.read_file('./shapefiles/fortal118.shp')
shp.crs = "+proj=utm +zone=24 +ellps=WGS84 +datum=WGS84 +units=m +no_defs +south"
shp = shp.to_crs("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
Map = folium.Map(location=[-3.731862, -38.526669], zoom_start=12, tiles='cartodbpositron')
folium.GeoJson(shp).add_to(Map)
# loading the data to use in order to make the heatmap
cases2011 = pd.read_csv('cases2011.txt', sep=" ", names=['day', 'month', 'lon', 'lat', 'label'], usecols=['lon', 'lat'])
cases2011['amount'] = np.random.randint(0, 100, size=len(cases2011))
# constructing the heatmap and adding it to the folium map
max_amount = float(cases2011['amount'].max())
hm_wide = HeatMap(zip(cases2011.lat.values, cases2011.lon.values, cases2011.amount.values),
min_opacity=0.1,
max_val=max_amount,
radius=17, blur=15,
max_zoom=10)
Map.add_child(hm_wide)
# showing the figure
Map
The issue is that it was supposed to show a figure with the folium map, the shapefile and the heatmap generated. The heatmap is not displayed, just the folium map and the shapefile appears.
The expected output should be similar to the figure of this post tutorial https://alcidanalytics.com/p/geographic-heatmap-in-python
folium.__version__0.5.0
Could anyone help me with this issue, please?
Couple of things:
pip install https://github.com/python-visualization/folium.git.Hi @Conengmo , thank you for you reply!
Alright I found the problem, it's with using zip. The Heatmap class cannot handle that and it stays a zip object in unpacking. Unfortunately it fails silently.
You can solve this by converting it into a list before creating Heatmap.
data = list(zip(cases2011.lat.values, cases2011.lon.values, cases2011.amount.values))
hm_wide = HeatMap(data)
We should add a validation step in the initialization of Heatmap that checks if the data argument is a valid type (iterable or array or maybe also pandas types?) and has a valid shape.
Does somebody want to make a PR with this?
Thank you very much for your help! Indeed, I could just run successfully using a small dataset. Using the one I attached, neither the map nor the heatmap does not load. I think that issue did not occur with you. Do you think it is an issue with the browser?
Could be. Could you try saving the map as html and opening that? We know there are some issues with folium + Jupyter + Chrome.
This is the code I use to create your heatmap and store the html file. I removed the shapefile part because you mentioned it wasn't part of the problem.
import pandas as pd
import numpy as np
import folium
from folium.plugins import HeatMap
Map = folium.Map(location=[-3.731862, -38.526669], zoom_start=12, tiles='cartodbpositron')
# loading the data to use in order to make the heatmap
cases2011 = pd.read_csv('cases2011.txt', sep=" ", usecols=['lon', 'lat'],
names=['day', 'month', 'lon', 'lat', 'label'], )
cases2011['amount'] = np.random.randint(0, 100, size=len(cases2011))
# constructing the heatmap and adding it to the folium map
max_amount = float(cases2011['amount'].max())
data = list(zip(cases2011.lat.values, cases2011.lon.values, cases2011.amount.values))
hm_wide = HeatMap(data, min_opacity=0.1, max_val=max_amount, radius=17,
blur=15, max_zoom=10)
Map.add_child(hm_wide)
Map.save('_map.html')
By saving it, it works! Thank you very much!
Most helpful comment
We should add a validation step in the initialization of
Heatmapthat checks if thedataargument is a valid type (iterable or array or maybe also pandas types?) and has a valid shape.Does somebody want to make a PR with this?