Python 我如何在地图上画圆圈?
这是我的数据框:Python 我如何在地图上画圆圈?,python,Python,这是我的数据框: Boston Zipcode Employees Latitude Longitude 0 02021 174 -71.131057 42.228065 1 02026 193 -71.143038 42.237719 3 02109 45 -71.054027 42.363498 4 02110 14 -71.053642 42.357649 5 02111 30 -71.060280 42.350586 6
Boston
Zipcode Employees Latitude Longitude
0 02021 174 -71.131057 42.228065
1 02026 193 -71.143038 42.237719
3 02109 45 -71.054027 42.363498
4 02110 14 -71.053642 42.357649
5 02111 30 -71.060280 42.350586
6 02113 77 -71.054618 42.365215
8 02115 116 -71.095106 42.343330
10 02118 318 -71.072103 42.339342
11 02119 804 -71.085268 42.323002
12 02120 168 -71.097569 42.332539
13 02121 781 -71.086649 42.305792
15 02124 1938 -71.066702 42.281721
16 02125 859 -71.053049 42.310813
17 02126 882 -71.090424 42.272444
19 02128 786 -71.016037 42.375254
21 02130 886 -71.114080 42.309087
22 02131 1222 -71.121464 42.285216
23 02132 1348 -71.168150 42.280316
24 02134 230 -71.123323 42.355355
25 02135 584 -71.147046 42.357537
26 02136 1712 -71.125550 42.255064
28 02152 119 -70.960324 42.351129
29 02163 1 -71.120420 42.367263
30 02186 361 -71.113223 42.258883
31 02199 4 -71.082279 42.346991
32 02210 35 -71.044281 42.347148
33 02215 83 -71.103877 42.348709
34 02459 27 -71.187563 42.286356
35 02467 66 -71.157691 42.314277
我想在地图上画圆圈,每个圆圈对应一个点,圆圈的大小取决于员工的数量
以下是我的地图代码(我尝试使用marker,但我认为circle更好:
boston_map=folium.Map([Boston['Longitude'].mean(), Boston['Latitude'].mean()],zoom_start=12)
incidents2=plugins.MarkerCluster().add_to(boston_map)
for Latitude,Longitude,Employees in zip(Boston.Latitude,Boston.Longitude,Boston.Employees):
folium.Marker(location=[Latitude,Longitude],icon=None,popup=Employees).add_to(incidents2)
boston_map.add_child(incidents2)
boston_map
这是我的地图:
如果员工人数可以显示在圆圈中,那就更好了!非常感谢!要画圆圈,您可以用它来代替
标记
顺便说一句:您有错误的列名。有
lat:42.361145,long:-71.057083
但您在经度列中有42
值,在纬度列中有-71
值
因为我不使用Juputer,所以我将地图保存在HTML文件中,并使用webbrowser
在web浏览器中自动打开它
因为它创建了大圆圈,所以我将员工
划分成更小的圆圈。但现在一些圆圈非常小,它显示的是圆圈的数量,而不是圆圈。也许应该使用math.log()
或其他方法使其变小(标准化)
我使用tooltip=str(employees)
在您将鼠标悬停在圆圈上时显示数字
text = '''
Zipcode Employees Longitude Latitude
0 02021 174 -71.131057 42.228065
1 02026 193 -71.143038 42.237719
3 02109 45 -71.054027 42.363498
4 02110 14 -71.053642 42.357649
5 02111 30 -71.060280 42.350586
6 02113 77 -71.054618 42.365215
8 02115 116 -71.095106 42.343330
10 02118 318 -71.072103 42.339342
11 02119 804 -71.085268 42.323002
12 02120 168 -71.097569 42.332539
13 02121 781 -71.086649 42.305792
15 02124 1938 -71.066702 42.281721
16 02125 859 -71.053049 42.310813
17 02126 882 -71.090424 42.272444
19 02128 786 -71.016037 42.375254
21 02130 886 -71.114080 42.309087
22 02131 1222 -71.121464 42.285216
23 02132 1348 -71.168150 42.280316
24 02134 230 -71.123323 42.355355
25 02135 584 -71.147046 42.357537
26 02136 1712 -71.125550 42.255064
28 02152 119 -70.960324 42.351129
29 02163 1 -71.120420 42.367263
30 02186 361 -71.113223 42.258883
31 02199 4 -71.082279 42.346991
32 02210 35 -71.044281 42.347148
33 02215 83 -71.103877 42.348709
34 02459 27 -71.187563 42.286356
35 02467 66 -71.157691 42.314277
'''
import pandas as pd
import io
import folium
import folium.plugins
boston = pd.read_csv(io.StringIO(text), sep='\s+')
boston_map = folium.Map([boston.Latitude.mean(), boston.Longitude.mean(), ], zoom_start=12)
incidents2 = folium.plugins.MarkerCluster().add_to(boston_map)
for latitude, longitude, employees in zip(boston.Latitude, boston.Longitude, boston.Employees):
print(latitude, longitude, employees)
folium.vector_layers.CircleMarker(
location=[latitude, longitude],
tooltip=str(employees),
radius=employees/10,
color='#3186cc',
fill=True,
fill_color='#3186cc'
).add_to(incidents2)
boston_map.add_child(incidents2)
# display in web browser
import webbrowser
boston_map.save('map.html')
webbrowser.open('map.html')
编辑:问题的答案显示了如何使用标记和图标=DivIcon(text)
来添加文本,但它并没有像我预期的那样工作。你在谷歌上发现了什么?几秒钟后,我在谷歌上发现了:顺便说一句,第七个例子:首先你在地图()中使用经度,纬度
但后来您在标记()
中以不同的顺序使用了纬度、经度
。可能您在数据中有错误的列名。lat:42.361145
,long:-71.057083。
但在经度
列中有值42
,我添加了工具提示=str(员工),将鼠标悬停在圆圈上时显示nubmer。