You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.0 KiB

4 weeks ago
import pandas as pd
import folium
from folium.plugins import HeatMap
import numpy as np
# 1. 定义朱诺市的中心坐标
juneau_center = [58.3019, -134.4197]
# 2. 生成模拟数据(确保权重是数值类型)
np.random.seed(42)
num_points = 50
lats = np.random.normal(juneau_center[0], 0.1, num_points)
lons = np.random.normal(juneau_center[1], 0.1, num_points)
weights = np.random.rand(num_points) # 确保权重是浮点数
data = [[lat, lon, weight] for lat, lon, weight in zip(lats, lons, weights)]
# 3. 创建地图对象
m = folium.Map(
location=juneau_center,
zoom_start=12,
tiles="cartodbpositron"
)
# 4. 修复关键点:将 gradient 的键改为字符串格式
HeatMap(
data,
radius=20,
blur=15,
max_zoom=13,
gradient={'0.4': 'blue', '0.6': 'lime', '1': 'red'} # 键改为字符串
).add_to(m)
# 5. 标记市中心
folium.Marker(
location=juneau_center,
popup="Juneau, Alaska",
icon=folium.Icon(color="green")
).add_to(m)
# 6. 保存地图
m.save("juneau_heatmap.html")
# 在Jupyter中显示
# m