Folium: HeatMap doesn't use weights

Created on 12 Mar 2020  路  21Comments  路  Source: python-visualization/folium

I spend quite some time tracking down this bug. I think it should be fixed or the limitation should be clearly stated in the docs. This is a major time waster and makes a bad impression on this otherwise great package.

This is a major bug coming for the upstream Leaflet.heat project.

Problem description

The plugins.HeatMap states that the points used to create the heat map can
be weighted.

data (list of points of the form [lat, lng] or [lat, lng, weight])

However weights are simply ignored. This makes it useless for heatmaps that are based on a few points with different weights.

Expected Output

Weight should have impact. Or remove this option from the docs.

bug work in progress

All 21 comments

Hi @ibayer , sorry to hear this bug caused you some issues. I opened a PR which applies a patch from the leaflet.heat repo. Can you perhaps help by testing the PR, see if it solves the issue for you?

Alternatively, if we don't get a fix working, we should indeed update the documentation.

@ibayer

Thanks for looking into this! Sorry, project has moved on and I don't have any code handy to
reproduce / test your fix. When I get the opportunity I'll for sure give folium another shot in the future but that might take a while.

No problem, then thanks for the bug report :)

Hi i Have the same problem so far, what is the solution?

We have a fix pending, I reckon it will be in the next release. In the meantime you could test it out by manually applying it: https://github.com/python-visualization/folium/pull/1282/files

Conengmo this work in R?

No this is a Python library

Hello! I have the same problem - is the best solution still manually applying it? When is the next released planned for?

We have a fix pending that I have to finish up. When that's merged you can install folium from the master branch. A new release (v0.12.0) is not yet scheduled, but will follow reasonably soon.

You could help out by testing the fix and reporting if it worked for you: https://github.com/python-visualization/folium/pull/1282

Still not working correctly. I would expect another output.
Please do try the following code to see the problem:

m = folium.Map((0.5, 0.5), zoom_start=8, tiles=None)
HeatMap(
# make five dots with different weights: 1, 2, 3, 4 and 10
data=[(0, 0, 1),
(0.5, 0, 2),
(1.0, 0, 3),
(1.5, 0, 4),
(2.0, 0, 10)] , radius=15).add_to(m)
m

Just on sphere is bigger, there is no continuous gradient between 1 and 10.

@rbiele02 which folium version are you using?

Thanks @Conengmo for looking into this!
I am using Version: 0.11.0+19.g46f820e.
Somehow it uses weights, but not correctly. In the attached figure you can see that 4 circles are equally weighted and one has higher intensity. However I would expect something else when choosing the weights 1,2,3,4 and 10.
A similar behaviour can be found for example for weights 0.1, 0.2, 0.3, 0.4, 0.8. The first 4 are equally and only the last has a higher intensity.

Screenshot 2020-08-12 at 14 02 15

Weights in heatmaps can work a bit non-intuitive. I suspect that one value of 10 is so high that the other values are all small relatively. Please try a range where the values are closer together and see how that looks. For example some values between 1 and 2.

We have a test that verifies the behavior when using weights, so I'm fairly sure it's not broken. In it we create four points with weights 1, 1, 1.5 and 2: https://github.com/python-visualization/folium/pull/1282/files#diff-b6e9c94435e2e8e2a966d436a06a52b6R16 The result looks like this: https://github.com/Conengmo/folium/blob/cce48dc1fd46e23fefd84d6011c6a132f08ad006/tests/selenium/test_heat_map_selenium_screenshot.png

Thanks for the fast response.
When I use weights of {1, 2, 3, 4, 5} it seems to work.
1_2_3_4_5

However, when I use {0.1, 0.2, 0.3, 0.4, 0.5} I dont see any effect at all.
0 1_0 2_0 3_0 4_0 5

Would be nice to know in which range the weights needs to be to see weighting having effect. It might be a good idea to normalise the given weights to the range where it works.

Can you try {0.5, 1, 1.5} ? Maybe it assumes a minimum weight of 1...

Looking at the plugin documenation it seems to suggest values below 1 are valid: https://github.com/Leaflet/Leaflet.heat

I'm hesitant to do normalization, because I don't think we can determine an optimal range automatically. That depends not only on the weights, but also on the locations of the points, the zoom level and the radius settings.

{0.5, 1, 1.5} works very well!
{0, 0.5, 1.0} not really. Maybe it needs to be centred around 1? Also {0.8,1.0,1.2} looks reasonable.

I can shift my distribution to have a mean of 1. This might work.
Thanks a lot!

I think it's safe to say it assumes a default weight of 1 then.

Hi, I am new to this subject but I've tried playing with the min_opacity and the max_value and this works fine :

m = folium.Map((0.5, 0.5), zoom_start=8, tiles=None)
HeatMap(
# make five dots with different weights: 1, 2, 3, 4 and 5
data=[(0  , 0, 0.1),
      (0.5, 0, 0.2),
      (1.0, 0, 0.3),
      (1.5, 0, 0.4),
      (2.0, 0, 0.5)] , radius=25, blur = 10, min_opacity = 0, max_val = 0.0005).add_to(m)
m

It seems that max_value works like a normalisation. So if you want to display value between 1 and 5, you have to put the max_value at 0.005 and so on...

I'm using follium 0.11.0

Note that you're using v0.11.0 and we were talking about the master branch, which contains a fix for heatmaps and changes its behavior slightly. With the fix, max_val is deprecated.

Thanks @Conengmo I didn't get it. Your patch automatically set the max_val depending on the intensity of the data ?
How can I use it ?

You can install folium with pip from the master branch like this: pip install git+https://github.com/python-visualization/folium.git

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Alcampopiano picture Alcampopiano  路  14Comments

AntonioLopardo picture AntonioLopardo  路  15Comments

wangchenwc picture wangchenwc  路  18Comments

b7j picture b7j  路  13Comments

reaganch picture reaganch  路  13Comments