【问题标题】:Hovering Data for Choropleth maps In FoliumFolium 中 Choropleth 地图的悬停数据
【发布时间】:2021-04-15 23:44:06
【问题描述】:

我正在尝试使用 Folium 在 python 中制作 Choropleth 地图,以跟踪进入每个州的难民数量。我的地图正在运行,但我想创建标签以显示州名称和当我悬停/单击每个州时出现的难民数量。我不确定这是否可能,但我将不胜感激!这是我的跑步地图Choropleth map的代码。

import pandas as pd
import json
import folium 

twentysa=pd.read_excel("State_abrv (9).xlsx")

url = (
    "https://raw.githubusercontent.com/python-visualization/folium/master/examples/data"
)
state_geo = f"{url}/us-states.json"

h = folium.Map(location=[48, -102], zoom_start=3)

folium.Choropleth(
    geo_data=state_geo,
    name="2013 Data",
    data=thirteensa,
    columns=["State", "Refugee Influx"],
    key_on="feature.id",
    fill_color="BuPu",
    fill_opacity=0.8,
    line_opacity=0.3,
    legend_name="2013 Refugee Influx",
).add_to(h)
h

【问题讨论】:

标签: python folium choropleth


【解决方案1】:

您需要使用 folium.GeoJSON 来使用工具提示来启用悬停功能。我从here 借用了以下代码,它对我有用。这是允许在悬停时显示状态的起点,但也可以对其进行改进以显示难民人数。

# hover functionality

style_function = lambda x: {'fillColor': '#ffffff', 
                            'color':'#000000', 
                            'fillOpacity': 0.1, 
                            'weight': 0.1}
highlight_function = lambda x: {'fillColor': '#000000', 
                                'color':'#000000', 
                                'fillOpacity': 0.50, 
                                'weight': 0.1}
NIL = folium.features.GeoJson(
    state_geo,
    style_function=style_function, 
    control=False,
    highlight_function=highlight_function, 
    tooltip=folium.features.GeoJsonTooltip(
        fields=['name','id'],  # use fields from the json file
        aliases=['State: ','ID: '],
        style=("background-color: white; color: #333333; font-family: arial; font-size: 12px; padding: 10px;") 
    )
)
h.add_child(NIL)
h.keep_in_front(NIL)
folium.LayerControl().add_to(h)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-13
    • 1970-01-01
    • 2016-05-16
    • 2022-09-28
    • 1970-01-01
    • 1970-01-01
    • 2019-06-09
    • 1970-01-01
    相关资源
    最近更新 更多