如何在Folium中实现这一目标?

时间:2017-10-17 18:24:37

标签: python visualization folium

我正在使用Folium策划大地震。我可以绘制所有实例,大约25000个。但地图开始看起来非常笨拙。我使用了以下代码:

map1 = folium.Map(location=[80., -180], zoom_start=1)
def color(magnitude):
if magnitude<6:
    col='green'
elif [(magnitude>6)&(magnitude<7.5)]:
    col='yellow'
else:
    col='red'
return col
fg=folium.FeatureGroup(name="Earthquake Locations")
for latitude,longitude,magnitude in zip(earthquakes['Latitude'][:30],earthquakes['Longitude'][:30],earthquakes['Magnitude'][:30]):
fg.add_child(folium.Marker(location=[latitude,longitude],popup=(folium.Popup(magnitude)),icon=folium.Icon(color=color(magnitude))))
map1.add_child(fg)

现在我想让情节看起来像下面R笔记本中的第一个情节: Notebook

有人可以帮助我实现这样的情节,在各个点聚集的地方,当我们放大时,这些点会出现。

由于

2 个答案:

答案 0 :(得分:1)

是的,你可以。 MarkerCluster就是您追求的目标

给它一个旋转

import folium
from folium.plugins import MarkerCluster

map1 = folium.Map(location=[80., -180], zoom_start=1)

def color(magnitude):
    if magnitude<6:
        col='green'
    elif [(magnitude>6)&(magnitude<7.5)]:
        col='yellow'
    else:
        col='red'
    return col

map1.add_child(MarkerCluster(locations=list(zip(earthquakes['Latitude'], 
                                 earthquakes['Longitude'])),
                                 popups=earthquakes['Magnitude'].astype(str),
                                 icons=[color(m) for m in earthquakes['Magnitude']]))

map1

enter image description here

答案 1 :(得分:1)

Bob Haffer的代码也有效。我试图调整并编写另一段代码:

import folium
import folium.plugins
map1 = folium.Map(width=1000,height=500,location=[80, -180],tiles='CartoDB dark_matter',zoom_start=1)
def color(magnitude):
if magnitude<6:
    col='green'
else: 
    col='red'
return col
marker_cluster = folium.plugins.MarkerCluster().add_to(map1)
for point in locationlist.index:
    folium.Marker(list(locationlist.loc[point].values),popup='Magnitude:'+str(locationlist_pop[point]),icon=folium.Icon(color=color(locationlist_pop[point]))).add_to(marker_cluster)
marker_cluster = folium.plugins.MarkerCluster().add_to(map1)
map1

Output