当我使用Python folium库将鼠标悬停在世界地图上时,如何显示国家名称和人口?
我使用folium用python制作了一个网络地图。地图读取包含国家名称和人口编号的population.json文件,并在浏览器上显示地图 代码如下:当我使用Python folium库将鼠标悬停在世界地图上时,如何显示国家名称和人口?,python,python-3.x,tooltip,geojson,folium,Python,Python 3.x,Tooltip,Geojson,Folium,我使用folium用python制作了一个网络地图。地图读取包含国家名称和人口编号的population.json文件,并在浏览器上显示地图 代码如下: import pandas import folium map = folium.Map(location=[32, 0], zoom_start=4.3, tiles = "CartoDB positron", max_zoom = 100) fgp = folium.FeatureGroup(name="Population" )
import pandas
import folium
map = folium.Map(location=[32, 0], zoom_start=4.3, tiles = "CartoDB positron", max_zoom = 100)
fgp = folium.FeatureGroup(name="Population" )
def colorPicker(population):
if population < 10000000:
return 'green'
elif population >= 10000000 and population < 500000000:
return 'orange'
else:
return 'red'
fgp.add_child(folium.GeoJson(data=open('population.json', 'r', encoding='utf-8-sig').read(),
style_function=lambda x: {'fillColor': colorPicker(x['properties']['POP2005'])},
tooltip=lambda x: '%s\n%s' % (x['properties']['Name'], x['properties']['POP2005'])
))
map.add_child(fgp)
map.save("index.html")
我想要的是,每当用户在一个国家上空盘旋时,我都想显示该国家的名称和该国的人口规模。为此,我写道:
tooltip=lambda x: '%s\n%s' % (x['properties']['Name'], x['properties']['POP2005'])
它没有给我国家的名字,而是给了我这个。。。
它应该说“中国:人口规模”,但实际上是“0x24…”
我不知道为什么。我尝试了几种不同的工具提示,例如:
tooltip=lambda x: '{0}\n{1}'.format(x['properties']['Name'], x['properties']['POP2005'])
tooltip=lambda x: '%s\n%s' % (x['properties']['Name'], x['properties']['POP2005'])
tooltip= lambda x: {'text': x['properties']['Name']}))
tooltip= lambda x: {'%s': x['properties']['Name']}))
但仍然显示相同的输出
下面是指向population.json文件的链接:使用和类:
importfolium
m=folium.Map(位置=[32,0],
缩放_开始=4.3,
tiles=“CartoDB正电子”,
最大(最大缩放=100)
def颜色选择器(总体):
如果人口<10000000:
返回“绿色”
elif人口>=10000000且人口<500000000:
返回“橙色”
其他:
返回“红色”
folium.GeoJson(open('population.json','r',encoding='utf-8-sig').read(),
名称='总体',
style_function=lambda x:{'fillColor':颜色选择器(x['properties']['POP2005']),
tooltip=folium.geojsontoltip(字段=('NAME','POP2005',),
别名=(“国家”,“人口”),
show=True)。将_添加到(m)
#folium.LayerControl().添加到(m)
M
你会得到:
使用和分类:
importfolium
m=folium.Map(位置=[32,0],
缩放_开始=4.3,
tiles=“CartoDB正电子”,
最大(最大缩放=100)
def颜色选择器(总体):
如果人口<10000000:
返回“绿色”
elif人口>=10000000且人口<500000000:
返回“橙色”
其他:
返回“红色”
folium.GeoJson(open('population.json','r',encoding='utf-8-sig').read(),
名称='总体',
style_function=lambda x:{'fillColor':颜色选择器(x['properties']['POP2005']),
tooltip=folium.geojsontoltip(字段=('NAME','POP2005',),
别名=(“国家”,“人口”),
show=True)。将_添加到(m)
#folium.LayerControl().添加到(m)
M
你会得到:
请提供
population.json
文件。谢谢。请提供population.json
文件。谢谢
tooltip=lambda x: '{0}\n{1}'.format(x['properties']['Name'], x['properties']['POP2005'])
tooltip=lambda x: '%s\n%s' % (x['properties']['Name'], x['properties']['POP2005'])
tooltip= lambda x: {'text': x['properties']['Name']}))
tooltip= lambda x: {'%s': x['properties']['Name']}))