Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify gradient parameter to folium heatmap?

Tags:

python

folium

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
like image 536
zesla Avatar asked Apr 10 '17 11:04

zesla


2 Answers

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.

like image 65
ivan7707 Avatar answered Nov 14 '22 21:11

ivan7707


From the JS template code of folium, the values of gradient should be between 0 to 1. eg:

gradient={'0':'Navy', '0.25':'Blue','0.5':'Green', '0.75':'Yellow','1': 'Red'}

https://cdn.jsdelivr.net/gh/python-visualization/folium@master/folium/templates/leaflet_heat.min.js

like image 45
Yuchi Pu Avatar answered Nov 14 '22 21:11

Yuchi Pu