https://github.com/WangZhangyu/Interactive_map
Folium
folium是python非常好用的交互式地图工具包,今天我们用folium来绘制简单精美的地震分布热力图。
我们选择川滇部分地区,蓝色方框圈定的区域进行绘制。橙色圆圈代表的是AETA 观测台站,黑色圆圈代表的是地震(ML3.5以上),黑色圆圈越大,震级越大。绘制地震分布的热力图,我们可以非常清晰的看到地震的集中分布情况。
绘制结果以.html的格式保存,我们可以对地图随意的放大缩小,当鼠标停留在黑色圆圈(地震)上时,地图会显示地震的发震时间和震级信息;当鼠标停留在橙色圆圈(台站)上时,会显示对应的台站名称。
- 安装Folium 库
pip install folium
- 导入必要的库
import folium
import pandas as pd
import webbrowser
from folium.plugins import HeatMap
- 加载AETA台站的信息并显示
cdata = pd.read_csv('StationInfo.csv')
for lat, lon,label in zip(cdata.Latitude, cdata.Longitude, cdata.Title):
incidents.add_child(
folium.CircleMarker(
[lat, lon],
radius=7,
color='yellow',
fill=True,
fill_color='red',
fill_opacity=0.4,
tooltip=label
)
)
- 加载并绘制地震目录信息
eq = pd.read_csv('eqlst.csv')
for lat, lon, mag, time in zip(eq.Latitude, eq.Longitude, eq.Magnitude,eq.Time):
incidents.add_child([图片上传中...(catalog.jpg-6f9441-1618157345666-0)]
folium.CircleMarker(
[lat, lon],
radius=(mag-3)*4,
color='black',
fill=True,
fill_color='black',
fill_opacity=0.5,
weight = 0.001,
stroke = False,
tooltip= '%s ML %.f' % (time,mag)
)
)
- 绘制地震分布热力图
heatdata = eq[['Latitude','Longitude','Magnitude']].values.tolist()
HeatMap(heatdata).add_to(incidents)
- 完整代码
"""
Created on Sun Apr 11 20:40:32 2021
@author: Zhangyu Wang
"""
import folium
import pandas as pd
import webbrowser
from folium.plugins import HeatMap
# Chuandain latitude and longitude
latitude = 28
longitude = 102.5
# instantiate a feature group for the incidents in the dataframe
incidents = folium.map.FeatureGroup()
# read stations
cdata = pd.read_csv('StationInfo.csv')
for lat, lon,label in zip(cdata.Latitude, cdata.Longitude, cdata.Title):
incidents.add_child(
folium.CircleMarker(
[lat, lon],
radius=7,
color='yellow',
fill=True,
fill_color='red',
fill_opacity=0.4,
tooltip=label
)
)
# read catalog
eq = pd.read_csv('eqlst.csv')
for lat, lon, mag, time in zip(eq.Latitude, eq.Longitude, eq.Magnitude,eq.Time):
incidents.add_child(
folium.CircleMarker(
[lat, lon],
radius=(mag-3)*4,
color='black',
fill=True,
fill_color='black',
fill_opacity=0.5,
weight = 0.001,
stroke = False,
tooltip= '%s ML %.f' % (time,mag)
)
)
# catalog heatmap
heatdata = eq[['Latitude','Longitude','Magnitude']].values.tolist()
HeatMap(heatdata).add_to(incidents)
# add a single earthquake
incidents.add_child(folium.Marker([28.9, 103.3],icon=folium.Icon(color='red'), tooltip='M = 4.0'))
# add area border
ls = folium.PolyLine(locations=[[22,98],[22,107],[34,107],[34,98],[22,98]],color='blue')
ls.add_to(incidents)
# add incidents to map
my_map = folium.Map(location=[latitude, longitude], zoom_start=6)
my_map.add_child(incidents)
# add latitude and longitude in the map when click
my_map.add_child(folium.LatLngPopup())
# save map
my_map.save('Chuandian_aeta.html')
webbrowser.open('Chuandian_aeta.html')