python-visualization / folium

Python Data. Leaflet.js Maps.
https://python-visualization.github.io/folium/
MIT License
6.85k stars 2.22k forks source link

Heatmap erratic behaviors #1505

Closed BriceChivu closed 1 year ago

BriceChivu commented 3 years ago

Description of the bug: 1) If we want to include weights for the heatmap, we have to normalize (log2 works well) our weights column otherwise it won't produce expected results. This could be added as a warning in the documentation. 2) The distance between points is considered much more significant than their weight. For example, 2 points close to each other with a weight of 1 and 2 respectively will be a "hotter area" than a single point with a weight equal to 5. 3) Zooming in or out produces different results (see screenshots below)

For reproducing purposes

# Data to test
test = pd.DataFrame(np.array([['Rotterdam', 'NL', 51.9228934, 4.4631786, 1.0],
       ['Alkmaar', 'NL', 52.63235473632812, 4.750678062438965,
        1.584962500721156],
       ['Augsburg', 'DE', 48.36598968505859, 10.89304447174072,
        6.643856189774724],
       ['Barcelona', 'ES', 41.3828939, 2.1774322, 9.643856189774725],
       ['Beauvais', 'FR', 49.42929840087891, 2.08105993270874,
        10.965784284662087]], dtype=object), columns=['City', 'Country', 'Latitude', 'Longitude', 'Weight'])

# Define the function to create the heatmap 
def test_heatmap(df_cog):
    # Instantiate map
    m = folium.Map(location=df_cog[['Latitude', 'Longitude']].mean(),
                   fit_bounds=[[df_cog['Latitude'].min(),
                                df_cog['Longitude'].min()],
                               [df_cog['Latitude'].max(),
                                df_cog['Longitude'].max()]])
    plugins.HeatMap(df_cog[['Latitude', 'Longitude', 'Volume']], name ='Heatmap').add_to(m)
    # Return the map
    return m

# Plot the map
m_1 = test_heatmap(df_cog=test)
m_1

Expected behavior I would expect an area to be shown as hot or cold depending solely on the sum of the weight of the points inside this area, and not the count of points.

Environment

Item 3) screenshots: look at Polska and Katowice image image image image

Conengmo commented 3 years ago

I advise you to try some different values for the radius and blur arguments. Possibly also max_zoom. I think it's expected that heatmaps require a bit of tweaking to have them look right, and that they may not work well on every zoom level.