How to create a geographical heatmap passing custom radii

这一生的挚爱 提交于 2020-03-23 04:00:07

问题


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

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?


回答1:


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.



来源:https://stackoverflow.com/questions/53174753/how-to-create-a-geographical-heatmap-passing-custom-radii

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!