How to specify gradient parameter to folium heatmap?

zesla picture zesla · Apr 10, 2017 · Viewed 7.1k times · Source

I'm making a heatmap for NYC apartment price using folium. I'm trying to use my own color gradient. When I specify the gradient argument in heatmap function, nothing shows on my map. Does anyone know how to generate our own color gradient and ideally a gradient bar on the map? Thanks a lot.

Here is my code: The first two columns of data are locations. The third column is price.

data =[[    40.7726,    -73.9568,   1900.    ],
       [    40.7785,    -73.9556,   3200.    ],
       [    40.7216,    -73.9809,   5800.    ],
       [    40.7384,    -73.9848,   2900.    ],
       [    40.7678,    -73.9915,   3312.    ],
       [    40.7659,    -73.9574,   2600.    ],
       [    40.7092,    -74.0137,   4299.    ],
       [    40.7384,    -73.982 ,   5750.    ],
       [    40.7312,    -73.9896,   3595.    ]]

from folium.plugins import HeatMap
hmap = folium.Map(location=[40.75, -73.97], tiles='stamentoner',control_scale = True, zoom_start=13)
hmap.add_child(HeatMap(data, radius = 10, gradient={1000: 'blue', 3000: 'lime', 5000: 'red'}))
hmap

Answer

ivan7707 picture ivan7707 · Apr 10, 2017

The gradient does not take the values of the magnitude as the dict keys for gradient.

Change

hmap.add_child(HeatMap(data, radius = 10, gradient={1000: 'blue', 3000: 'lime', 5000: 'red'}))

to

hmap.add_child(HeatMap(data, radius = 25, gradient={.4: 'blue', .65: 'lime', 1: 'red'}))

And it will work.