Search code examples
pythonnumpyheatmapnumpy-ndarrayfolium

Problem with Heatmap plotting by folium.plugins


I'm trying to use folium.plugins.Heatmap to generate a heatmap. But a tricky issue came up. I have my latitudes and longitudes in a ndarray as following:

latlongs = Chicago_Crime[['latitude','longitude']].to_numpy()
latlongs
array([[ 42.00167049, -87.67386364],
   [ 41.74736206, -87.70842371],
   [ 41.91786266, -87.74460059],
   ...,
   [ 41.68908506, -87.65078948],
   [ 41.75257724, -87.58211309],
   [ 41.75244573, -87.58088691]])

ThenI use the following codes for heatmap:

from folium import plugins
from folium.plugins import HeatMap

# let's start again with a clean copy of the map of Chicago
map_chicago = folium.Map(location=[latitude, longitude],zoom_start=10)

latlongs = Chicago_Crime[['latitude','longitude']].to_numpy()
HeatMap(latlongs).add_to(map_chicago)
map_chicago

However, some error relating to the size of the array I passed appeared and I just don't get it as I'm passing a (n,2) numpy array as required.

TypeError                                 Traceback (most recent call last)
<ipython-input-160-481a68e5e955> in <module>
      7 latlongs = Chicago_Crime[['latitude','longitude']].to_numpy()
      8 folium.TileLayer('cartodbpositron').add_to(map_chicago) #cartodbpositron cartodbdark_matter
----> 9 HeatMap(latlongs).add_to(map_chicago)
     10 map_chicago

~\anaconda3\lib\site-packages\folium\plugins\heat_map.py in __init__(self, data, name, min_opacity, max_zoom, max_val, radius, blur, gradient, overlay)
     43                  max_val=1.0, radius=25, blur=15, gradient=None, overlay=True):
     44         super(TileLayer, self).__init__(name=name)
---> 45         if _isnan(data):
     46             raise ValueError('data cannot contain NaNs, '
     47                              'got:\n{!r}'.format(data))

~\anaconda3\lib\site-packages\folium\utilities.py in _isnan(values)
     70 def _isnan(values):
     71     """Check if there are NaNs values in the iterable."""
---> 72     return any(math.isnan(value) for value in _flatten(values))
     73 
     74 

~\anaconda3\lib\site-packages\folium\utilities.py in <genexpr>(.0)
     70 def _isnan(values):
     71     """Check if there are NaNs values in the iterable."""
---> 72     return any(math.isnan(value) for value in _flatten(values))
     73 
     74 

TypeError: only size-1 arrays can be converted to Python scalars

What can I try next?


Solution

  • Example code show you how to add heatmap to on a map:

    import folium
    from folium.plugins import HeatMap
    import numpy as np
    
    latitude, longitude = 48, 5
    
    data = (
        np.random.normal(size=(100, 3)) *
        np.array([[1, 1, 1]]) +
        np.array([[48, 5, 1]])
    ).tolist()
    
    m = folium.Map([48, 5], zoom_start=10)
    
    HeatMap(data).add_to(folium.FeatureGroup(name='Heat Map').add_to(m))
    folium.LayerControl().add_to(m)
    
    m
    

    Output image is like:

    enter image description here