Python地理地图绘制:Folium与Altair的应用
立即解锁
发布时间: 2025-09-03 00:55:39 阅读量: 9 订阅数: 14 AIGC 

### Python地理地图绘制:Folium与Altair的应用
#### 1. Folium库简介
Folium是一个流行的Python图形库,用于绘制等值区域地图,可作为Plotly的替代方案。它与Plotly在逻辑和组织上有很多相似之处,但最大的优势在于与Leaflet的紧密集成。同时,Folium与geopandas结合使用,能简化配置并扩展功能。
```python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from PIL import Image
import json
import geopandas as gpd
import folium
```
#### 2. Folium基础图形元素
##### 2.1 基础地图
使用`folium.Map()`函数可以定义基础地图,需要指定地图中心的经纬度(`location`属性),还可以设置初始缩放级别(`zoom_start`)和地图大小(`width`和`height`)。若要添加瓦片地图,可使用`tiles`属性;对于商业瓦片地图,还需提供`API_key`。
```python
map1 = folium.Map(location=[40.78367, -73.96584], zoom_start=11, width=500, height=500)
map1.save('Map1.html')
```
流程图如下:
```mermaid
graph LR
A[开始] --> B[定义地图中心经纬度]
B --> C[设置缩放级别和地图大小]
C --> D[创建基础地图]
D --> E[保存地图为HTML文件]
E --> F[结束]
```
##### 2.2 标记
使用`folium.Marker()`函数添加标记,并结合`add_to()`方法将标记添加到基础地图上。可以通过`icon`属性和`folium.Icon()`函数设置标记的样式,如颜色和图标。同时,还能为标记关联弹出窗口(`popup`属性)和工具提示(`tooltip`属性)。
```python
folium.Marker([40.7116, -74.0132],
popup="<i>The World Trade Center and the National September 11th Memorial and Museum</i>",
tooltip="Ground Zero",
icon=folium.Icon(icon="building", prefix='fa', color='black')).add_to(map1)
```
操作步骤:
1. 确定标记的经纬度。
2. 设置弹出窗口和工具提示的内容。
3. 定义标记的样式。
4. 将标记添加到地图上。
表格展示部分标记信息:
| 地点 | 经纬度 | 弹出窗口内容 | 工具提示 | 标记颜色 | 标记图标 |
| --- | --- | --- | --- | --- | --- |
| 世界贸易中心 | [40.7116, -74.0132] | The World Trade Center and the National September 11th Memorial and Museum | Ground Zero | 黑色 | 建筑 |
| 自由女神像 | [40.6892, -74.0445] | The Statue of Liberty is a gift from the people of France | Statue of Liberty | 浅蓝色 | 船 |
##### 2.3 圆形元素
可以使用`folium.Circle()`或`folium.CircleMarker()`函数添加圆形元素,它们的使用方式与标记类似,只是形状为圆形。
#### 3. 高级工具提示和弹出窗口
Folium提供了丰富的工具提示配置选项,包括插入Vega/Altair图形、表格和图像。以下是一个在弹出窗口中包含图像的示例。
```python
import branca
map2 = folium.Map(location=[40.78367, -73.96584], zoom_start=11, width=500, height=500, tiles='Stamen Terrain')
# 世界贸易中心纪念馆
html1 = """<div> <img src="https://images.unsplash.com/photo-1495954283789-905ff632dca0?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=774&q=80" alt="The World Trade Center and the National September 11th Memorial and Museum" width=200 height=300 > <br/><span>The World Trade Center and the National September 11th Memorial and Museum</span> </div>"""
# 美国自然历史博物馆
html2 = """<div> <img src="https://images.unsplash.com/photo-1647962309326-b77f9732860e?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=774&q=80" alt="American Museum of Natural History" width=300 height=500 > <br/><span>American Museum of Natural History</span></div>"""
iframe1 = branca.element.IFrame(html=html1, width=250, height=350)
popup1 = folium.Popup(iframe1, max_width=250, max_height=1000)
iframe2 = branca.element.IFrame(html=html2, width=300, height=400)
popup2 = folium.Popup(iframe2, max_width=350, max_height=1000)
folium.Marker([40.7116, -74.0132], popup=popup1, tooltip="The World Trade Center and the National September 11th Memorial and Museum", icon=folium.Icon(icon="building", prefix='fa', color='black')).add_to(map2)
folium.Marker([40.7813, -73.9740], popup=popup2, tooltip="<b>American Museum of Natural History</b>", icon=folium.Icon(icon="institution", prefix='fa')).add_to(map2)
```
操作步骤:
1. 准备包含图像的HTML代码。
2. 使用`branca`库的`IFrame`函数创建内联框架。
3. 使用`folium.Popup`函数创建弹出窗口。
4. 将弹出窗口关联到标记上,并添加到地图中。
#### 4. 叠加图层和GeoJSON数据集
当需要将GeoJSON数据集中的图形元素图层叠加到Folium地图上时,可使用`folium.GeoJson()`函数加载数据,并使用`add_to()`方法将图层叠加到地图上。
```python
seaRise = json.load(open('datasets/FEMA/Se
```
0
0
复制全文
相关推荐










