惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
aimingoo的专栏
aimingoo的专栏
H
Help Net Security
L
LangChain Blog
M
MIT News - Artificial intelligence
The GitHub Blog
The GitHub Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
C
Check Point Blog
P
Proofpoint News Feed
J
Java Code Geeks
大猫的无限游戏
大猫的无限游戏
博客园_首页
Blog — PlanetScale
Blog — PlanetScale
U
Unit 42
I
InfoQ
月光博客
月光博客
爱范儿
爱范儿
Stack Overflow Blog
Stack Overflow Blog
V
Visual Studio Blog
Y
Y Combinator Blog
Microsoft Security Blog
Microsoft Security Blog
博客园 - Franky
D
Docker
B
Blog

又见苍岚

COLMAP PatchMatch Stereo 算法详解 事件驱动的状态机框架:从理论到工程实践 Git 在国内网络环境下无法 Push 的排查与修复 —— 配置 Clash 代理 分段五次多项式插值原理详解 路径插值方法深度对比研究 Claude Code 使用指南 OpenClaw 记忆管理与技能创建指南 CBS(Conflict-Based Search)算法详解 A* 算法及其变种详解 OpenClaw 配置多 Agents Windows Powershell 无法加载文件,因为在此系统上禁止运行脚本问题的解决方案 MaxClaw 安装流程 大模型 AI 名词介绍 AList 网盘聚合工具简介 Protobuf 简介与测试 Claude Code 简介以及 GLM 4.7 模型接入 Github 歌词下载工具 163MusicLyrics Python __getattr__ 懒加载 Python TypedDict 机器人仿真平台 Gazebo 安装记录 机器人仿真平台 Gazebo 简介 多机器人路径规划问题(Multi-Agent Path Finding, MAPF)简介 Python exifread 读取修改过的 jpeg 信息错误问题修复 3D 坐标系变换的理解 3D 旋转矩阵基本概念 MongoDB Compass 介绍 Python 环境管理工具 uv Flutter 开发指南 Snipaste 安装下载与黑屏问题解决方案 全局路径规划算法记录
Python 地图可视化 Folium
Yiwei Zhang · 2024-05-21 · via 又见苍岚

Folium 是一个基于 Leaflet 的 Python 库,用于将地理数据可视化为地图。Leaflet 是一个开源的交互式地图JavaScript库,而 Folium 则提供了一个简单的方式来将这些功能带到 Python 环境中。这对于数据科学家和分析师来说非常有用,因为他们可以在数据分析工作流程中轻松地创建、查看和分享地理空间数据。

folium 建立在 Python 生态系统的数据应用能力和 Leaflet.js 库的映射能力之上,在Python中操作数据,然后通过 folium 在 Leaflet 地图中可视化。folium 相比较于国内百度的 pyecharts 灵活性更强,能够自定义绘制区域,并且展现形式更加多样化。

这个开源库中有许多来自 OpenStreetMap、MapQuest Open、MapQuestOpen Aerial、Mapbox和Stamen 的内建地图元件,而且支持使用 Mapbox 或 Cloudmade 的 API 密钥来定制个性化的地图元件。Folium支持 GeoJSON 和 TopoJSON 两种文件格式的叠加,也可以将数据连接到这两种文件格式的叠加层,最后可使用 color-brewer 配色方案创建分布图。

Folium可以让你用 Python 强大生态系统来处理数据,然后用 Leaflet 地图来展示。Folium内置一些来自 OpenStreetMap、MapQuest Open、MapQuest Open Aerial、Mapbox和Stamen 的地图元件(tilesets),并且支持用 Mapbox 或者 Cloudmade API keys 来自定义地图元件。Folium支持 GeoJSON 和 TopJSON 叠加(overlays),绑定数据来创造一个分级统计图(Choropleth map)。但是,Folium库绘制热点图的时候,需要联网才可显示。

CircleMarker 以像素指定半径,而 Circle 以米指定。这意味着当你缩放时,CircleMarker 不会改变你屏幕上的大小,而 Circle 在地图上有一个固定的位置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Coordinates are 15 points on the great circle from Boston to San Francisco.
coordinates = [
[42.3581, -71.0636],
[42.82995815, -74.78991444],
[43.17929819, -78.56603306],
[43.40320216, -82.37774519],
[43.49975489, -86.20965845],
[43.46811941, -90.04569087],
[43.30857071, -93.86961818],
[43.02248456, -97.66563267],
[42.61228259, -101.41886832],
[42.08133868, -105.11585198],
[41.4338549, -108.74485069],
[40.67471747, -112.29609954],
[39.8093434, -115.76190821],
[38.84352776, -119.13665678],
[37.7833, -122.4167],
]

# Create the map and add the line
m = folium.Map(location=[41.9, -97.3], zoom_start=4)

folium.PolyLine(
locations=coordinates,
color="#FF0000",
weight=5,
tooltip="From Boston to San Francisco",
).add_to(m)

m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
lat = +38.89399
lon = -77.03659
zoom_start = 17

m = folium.Map(location=[lat, lon], zoom_start=zoom_start)

kw = {"color": "red", "fill": True, "radius": 20}

folium.CircleMarker([38.89415, -77.03738], **kw).add_to(m)
folium.CircleMarker([38.89415, -77.03578], **kw).add_to(m)

locations = [
[
(38.893596444352134, -77.03814983367920),
(38.893379333722040, -77.03792452812195),
],
[
(38.893379333722040, -77.03792452812195),
(38.893162222428310, -77.03761339187622),
],
[
(38.893162222428310, -77.03761339187622),
(38.893028615148424, -77.03731298446655),
],
[
(38.893028615148424, -77.03731298446655),
(38.892920059048464, -77.03691601753235),
],
[
(38.892920059048464, -77.03691601753235),
(38.892903358095296, -77.03637957572937),
],
[
(38.892903358095296, -77.03637957572937),
(38.893011914220770, -77.03592896461487),
],
[
(38.893011914220770, -77.03592896461487),
(38.893162222428310, -77.03549981117249),
],
[
(38.893162222428310, -77.03549981117249),
(38.893404384982480, -77.03514575958252),
],
[
(38.893404384982480, -77.03514575958252),
(38.893596444352134, -77.03496336936950),
],
]

folium.PolyLine(
locations=locations,
color="orange",
weight=8,
opacity=1,
smooth_factor=0,
).add_to(m)

m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
m = folium.Map(location=[35.685, 139.76], zoom_start=15)

kw = {
"color": "blue",
"line_cap": "round",
"fill": True,
"fill_color": "red",
"weight": 5,
"popup": "Tokyo, Japan",
"tooltip": "<strong>Click me!</strong>",
}

folium.Rectangle(
bounds=[[35.681, 139.766], [35.691, 139.776]],
line_join="round",
dash_array="5, 5",
**kw,
).add_to(m)

dx = 0.012
folium.Rectangle(
bounds=[[35.681, 139.766 - dx], [35.691, 139.776 - dx]],
line_join="mitter",
dash_array="5, 10",
**kw,
).add_to(m)

folium.Rectangle(
bounds=[[35.681, 139.766 - 2 * dx], [35.691, 139.7762 - 2 * dx]],
line_join="bevel",
dash_array="15, 10, 5, 10, 15",
**kw,
).add_to(m)

m