[英]How do I display country name and population when I hover over world map using Python folium library?
I made a Webmap with python using folium.我使用folium用python制作了一个Webmap。 The map reads in population.json file that contains country names, and population number and displays a map on browser.地图读取包含国家名称和人口数量的population.json文件,并在浏览器上显示地图。
Here's the code:这是代码:
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")
I created feature group and add_child to add colors to each country on the map based on their population size using this code:我创建了要素组和 add_child 以使用以下代码根据人口规模为地图上的每个国家/地区添加颜色:
style_function=lambda x: {'fillColor': colorPicker(x['properties']['POP2005'])}
What I wanted to then is whenever user hovers over a country, I want to display the name of the country and the population size of the country.我想要的是每当用户将鼠标悬停在一个国家上时,我想显示该国家/地区的名称和该国家/地区的人口规模。 For that, I wrote:为此,我写道:
tooltip=lambda x: '%s\n%s' % (x['properties']['Name'], x['properties']['POP2005'])
Instead of giving me the name of the country, it gives me this... Picture of map它没有给我国家的名字,而是给了我这个...地图图片
It suppose to say "China: 'population size'", but instead is shows "at 0x24...."它假设说“中国:'人口规模'”,而是显示“在 0x24 ....”
I'm not sure why.我不知道为什么。 I've tried several variations of tooltip such as:我尝试了几种工具提示的变体,例如:
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']}))
But still, shows me same output但是,仍然显示相同的输出
Here's link to population.json file: file这是population.json文件的链接: file
Use GeoJson
and GeoJsonTooltip
classes:使用GeoJson
和GeoJsonTooltip
类:
import folium
m = folium.Map(location=[32, 0],
zoom_start=4.3,
tiles = "CartoDB positron",
max_zoom = 100)
def colorPicker(population):
if population < 10000000:
return 'green'
elif population >= 10000000 and population < 500000000:
return 'orange'
else:
return 'red'
folium.GeoJson(open('population.json', 'r', encoding='utf-8-sig').read(),
name = 'Population',
style_function = lambda x: {'fillColor': colorPicker(x['properties']['POP2005'])},
tooltip = folium.GeoJsonTooltip(fields=('NAME', 'POP2005',),
aliases=('Country','Population')),
show = True).add_to(m)
#folium.LayerControl().add_to(m)
m
and you get:你会得到:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.