简体   繁体   English

当我使用 Python folium 库将鼠标悬停在世界地图上时,如何显示国家名称和人口?

[英]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:使用GeoJsonGeoJsonTooltip类:

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.

相关问题 如何使用 Pygal 和 Python 在世界地图中显示字符串数据? - How do I display string data in a world map using Pygal and Python? 使用pygal.maps.world时,是否可以格式化显示国家人口的数字? - When using pygal.maps.world is there a way to format the numbers that display a country's population? 如何使用 Python 在 HTML 页面上抓取仅在鼠标悬停在地图/图像部分上时显示的数据? - How do I web scrape data on an HTML page that only shows up on a mouse hover over parts of a map/image using Python? 如何在 Folium 的地图上添加“计数”? - How do I achieve adding "count" on the map in Folium? 如何使用 json 文件中的数据在大叶地图上创建弹出窗口? - How do I create a popup on folium map using data from json file? 如何让 Folium 以英文显示地图? - How do I get Folium to display maps in English? 我如何在Folium中实现这一目标? - How do I achieve this in Folium? 如何将 IOC 国家代码转换为国家名称? - How do I convert an IOC country code to country name? 如何使用 folium 在 Python 中保存 map 部分 - how to save a map section in Python using folium 如何返回 DataFrame 的行,其中每个大陆的每个国家/地区的人口都少于 100? - How do I return the rows of DataFrame where every Country in each Continent has a Population of less of than 100?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM