5

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
kame
  • 20,848
  • 33
  • 104
  • 159
zesla
  • 11,155
  • 16
  • 82
  • 147

2 Answers2

7

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.

ivan7707
  • 1,146
  • 1
  • 13
  • 24
  • Thanks a lot. So the gradient only takes percentage of the magnitude as the dictionary key? Do you know how I can display any gradient color bar on the side of map? @ivan7707 – zesla Apr 10 '17 at 13:42
  • @zesla I cannot see anything called a color bar in the API. [branca.colormap](https://python-visualization.github.io/folium/module/colormap.html) can be used to create what you need. That said, I am unclear on whether you can get it to the side (assuming you mean vertical ). – ivan7707 Apr 10 '17 at 18:16
  • 1
    Here is the new address of [branca color map](https://github.com/python-visualization/branca/blob/master/branca/colormap.py). @ivan7707 link is dead. – Kardi Teknomo Feb 22 '18 at 14:21
2

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

Yuchi Pu
  • 21
  • 2