Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a geographical heatmap passing custom radii

I want to create a visualization on a map using folium. In the map I want to observe how many items are related to a particular geographical point building a heatmap. Below is the code I'm using.

import pandas as pd
import folium
from folium import plugins

data = [[41.895278,12.482222,2873494.0,20.243001,20414,7.104243],
        [41.883850,12.333330,3916.0,0.835251,4,1.021450],
        [41.854241,12.567000,22263.0,1.132390,35,1.572115],
        [41.902147,12.590388,19505.0,0.839181,37,1.896950],
        [41.994240,12.48520,16239.0,1.383981,25,1.539504]]

df = pd.DataFrame(columns=['latitude','longitude','population','radius','count','normalized'],data=data)

middle_lat = df['latitude'].median()
middle_lon = df['longitude'].median()
m = folium.Map(location=[middle_lat, middle_lon],tiles = "Stamen Terrain",zoom_start=11)

# convert to (n, 2) nd-array format for heatmap
points = df[['latitude', 'longitude', 'normalized']].dropna().values

# plot heatmap
plugins.HeatMap(points, radius=15).add_to(m)
m.save(outfile='map.html')

Here the result

heatmap

In this map, each point has the same radius. Insted, I want to create a heatmap in which the points radius is proportional with the one of the city it belongs to. I already tried to pass the radii in a list, but it is not working, as well as passing the values with a for loop.

Any idea?

like image 284
el_Rinaldo Avatar asked Dec 11 '25 07:12

el_Rinaldo


1 Answers

You need to add one point after another. So you can specify the radius for each point. Like this:

import random
import numpy

pointArrays = numpy.split(points, len(points))
radii = [5, 10, 15, 20, 25]

for point, radius in zip(pointArrays, radii):
    plugins.HeatMap(point, radius=radius).add_to(m)

m.save(outfile='map.html')

Here you can see, each point has a different size.

Result

like image 78
Mr_Z Avatar answered Dec 13 '25 20:12

Mr_Z



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!