我们可以将值作为权重提供给 folium 中的 HeatMap(请参阅文档中的 data 参数)。
这是一个例子。我将框下半部分的值除以 5,以证明尽管数据点的密度相对均匀,但热图在图片的上半部分清楚地显示出更高的量级:
import pandas as pd
import numpy as np
import folium
from folium.plugins import HeatMap
import matplotlib as mpl
# parameters
n = 250 # number of points
lat0 = 40.7 # coordinates will be generated uniformly with
lon0 = -73.9 # lat0 - eps <= lat < lat0 + eps
eps = 0.1 # lon0 - eps <= lon < lon0 + eps
v_min, v_max = 0, 100 # min, max values
# generating values
lat = np.random.uniform(lat0 - eps, lat0 + eps, n)
lon = np.random.uniform(lon0 - eps, lon0 + eps, n)
value = numpy.random.uniform(v_min, v_max, n)
df = pandas.DataFrame({'lat': lat, 'lon': lon, 'value': value})
# to demonstrate the effect of weights on the heatmap,
# we'll divide values below the center of the box by K = 5
K = 5
df.loc[df['lat'] < lat0, 'value'] /= K
# plotting the map, both the points themselves and the heatmap
m = folium.Map(location = [lat0, lon0], tiles="OpenStreetMap",
zoom_start=11, width=400, height=400)
for elt in list(zip(df.lat, df.lon, df.value)):
folium.Circle(elt[:2], color="white", radius=elt[2]).add_to(m)
# df.values used here is a (250, 3) numpy.ndarray
# with (lat, lon, weight) for each data point
HeatMap(data=df.values, min_opacity=0.1).add_to(m)
m
输出:
更新:
这是一种稍微不同的方法,不使用内置的热图(根据@agenis 提到的https://github.com/python-visualization/folium/issues/1271,这可能是目前更好的选择)。
我们首先转换我们的数据,制作正方形网格,其中每个正方形的值是位于该单元格内的原始数据点值的平均值(因此它不取决于该单元格内的点数,仅取决于它们的值)。
然后我们可以通过在地图上绘制 GeoJson 多边形来可视化这些正方形。这是一个例子:
# define the size of the square
step = 0.02
# calculate values for the grid
x = df.copy()
x['lat'] = np.floor(x['lat'] / step) * step
x['lon'] = np.floor(x['lon'] / step) * step
x = x.groupby(['lat', 'lon'])['value'].mean()
x /= x.max()
x = x.reset_index()
# geo_json returns a single square
def geo_json(lat, lon, value, step):
cmap = mpl.cm.RdBu
return {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
'color': 'white',
'weight': 1,
'fillColor': mpl.colors.to_hex(cmap(value)),
'fillOpacity': 0.5,
},
"geometry": {
"type": "Polygon",
"coordinates": [[
[lon, lat],
[lon, lat + step],
[lon + step, lat + step],
[lon + step, lat],
[lon, lat],
]]}}]}
# generating a map...
m = folium.Map(location=[lat0, lon0], zoom_start=11, width=400, height=400)
# ...with squares...
for _, xi in x.iterrows():
folium.GeoJson(geo_json(xi['lat'], xi['lon'], xi['value'], step),
lambda x: x['properties']).add_to(m)
# ...and the original points
for elt in list(zip(df.lat, df.lon, df.value)):
folium.Circle(elt[:2], color="white", radius=elt[2]).add_to(m)
m
输出:
更新(2):
这是一个使用griddata在网格上进行插值的版本:
import pandas as pd
import numpy as np
import folium
from scipy.interpolate import griddata
# parameters
n = 250 # number of points
lat0 = 40.7
lon0 = -73.9
eps = 0.1
v_min, v_max = 0, 100 # min, max values
# generating values
lat = np.random.normal(lat0, eps, n)
lon = np.random.normal(lon0, eps, n)
value = numpy.random.uniform(v_min, v_max, n)
# set up the grid
step = 0.02
xi, yi = np.meshgrid(
np.arange(lat.min() - step/2, lat.max() + step/2, step),
np.arange(lon.min() - step/2, lon.max() + step/2, step),
)
# interpolate and normalize values
zi = griddata((lat, lon), value, (xi, yi), method='linear')
zi /= np.nanmax(zi)
g = np.stack([
xi.flatten(),
yi.flatten(),
zi.flatten(),
], axis=1)
# geo_json returns a single square
def geo_json(lat, lon, value, step):
cmap = mpl.cm.RdBu
return {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
'color': 'white',
'weight': 1,
'fillColor': mpl.colors.to_hex(cmap(value)),
'fillOpacity': 0.5,
},
"geometry": {
"type": "Polygon",
"coordinates": [[
[lon - step/2, lat - step/2],
[lon - step/2, lat + step/2],
[lon + step/2, lat + step/2],
[lon + step/2, lat - step/2],
[lon - step/2, lat - step/2],
]]}}]}
# generating a map...
m = folium.Map(location=[lat0, lon0], zoom_start=9, width=400, height=400)
# ...with squares...
for gi in g:
if ~np.isnan(gi[2]):
folium.GeoJson(geo_json(gi[0], gi[1], gi[2], step),
lambda x: x['properties']).add_to(m)
# ...and the original points
for elt in list(zip(lat, lon, value)):
folium.Circle(elt[:2], color='white', radius=elt[2]).add_to(m)
m
输出: