Folium: HeatMap fails silently on incompatible data type

Created on 9 Aug 2018  路  7Comments  路  Source: python-visualization/folium

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

Problem description

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.

Expected Output

The expected output should be similar to the figure of this post tutorial https://alcidanalytics.com/p/geographic-heatmap-in-python

Output of folium.__version__

0.5.0

Could anyone help me with this issue, please?

Necessary data

shapefiles.zip
cases2011.txt

bug

Most helpful comment

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?

All 7 comments

Couple of things:

  • You didn't include data in your example, so I can't run your code. Either make your code self contained or host your example on something like Github Gist or something.
  • Do you have an ad-blocker? Try to disable it when displaying the map. See #852.
  • Can you use the developer options of your browser to see if there are any error messages in the console?
  • Can you try it with the most recent version of folium? For that you have to install folium from the git master branch. Something like pip install https://github.com/python-visualization/folium.git.

Hi @Conengmo , thank you for you reply!

  • Sorry for the lack of the data, I included them now.
  • I don't have and ad-blocker.
  • I suppose by going into the developing mode, I should see the points of the heatmaps, especially the latlon coordinates, right? So, I could not see it.
  • I also installed the most recent version as you suggested and again, it did not work.

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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

EMarcantonio picture EMarcantonio  路  41Comments

wangchenwc picture wangchenwc  路  18Comments

agravier picture agravier  路  26Comments

ElmWer picture ElmWer  路  24Comments

ispmarin picture ispmarin  路  17Comments