Search code examples
pythonheatmapfolium

Python folium heatmap not changing with intensity


I am trying to change the normalized value of the gradient in a heatmap in the code below, but when I change the coefficient value the distribution of hot/cold does not change. It should be getting "hotter" or "colder" in spots depending on what I set that coefficient to. It's like it's not picking up the "weight" value at all. Can anyone tell me where I'm going wrong?

def generateBaseMap(loc, zoom=11, tiles='OpenStreetMap', crs='ESPG2263'):
    return folium.Map(location=loc, 
                      control_scale=True, 
                      zoom_start=zoom,
                      tiles=tiles)
  

MAP = [34.02571149, -118.366837957] 
  
base_map = generateBaseMap(MAP)


map_values = geo_stat_list_final[['ID','SecondID','Lat','Long','MoneyTotal']]
map_values['weight']=map_values['MoneyTotal']/5000


map_values1 = map_values[['Lat','Long','weight']]

data = map_values1.values.tolist()
           
hm = plugins.HeatMap(data,gradient={0.1: 'blue', 0.3: 'lime', 0.5: 'yellow', 0.7: 'orange', 1: 'red'}, 
                min_opacity=0.05, 
                max_opacity=0.9, 
                radius=8,
                use_local_extrema=False)
base_map.add_child(hm)

Solution

  • It seems that Folium has a critical flaw when it comes to heatmaps -- it completely disregards the "weight" or z characteristic. I've spent an enormous amount of time troubleshooting this issue, but finally came across this from github: https://github.com/python-visualization/folium/issues/1271 Apparently, Folium still ignores the weight of a data point even today. The "heat" of a map, is entirely due to the proximity of data points, not the strength of a characteristic.
    This is a major letdown and incredibly misleading for the unknowing person who produces heatmaps using Folium. I am looking for a solution for my heat maps outside of Folium.

    EDIT: It seems they HeatMapWithTime still works just as it should. For whatever reason the bug that prevents the regular heat maps from working does not affect the maps with time indexes. So if you're desperate for the zoom features in Folium, create a time index and use that with HeatMapWithTime. Alternatively, r-beginners' solution of using Plotly will work as well.