简体   繁体   English

将鼠标悬停在应出现文本的区域上

[英]Hover over an area a text should appear

I am currently viewing the neighborhoods.我目前正在查看社区。 However, when I hover the mouse over it, I want a text to be displayed with the neighborhood and the average price.但是,当我将鼠标悬停在它上面时,我希望显示一个带有社区和平均价格的文本。 I have pasted my code and my card what I made.我已经粘贴了我的代码和我制作的卡片。 Below you can see how I would like it.你可以在下面看到我想要的。

How can I display a text when I hover over it?将鼠标悬停在文本上时如何显示文本?

import folium
from folium.plugins import FastMarkerCluster
from branca.colormap import LinearColormap

map_ams_price = folium.Map(location=[52.3680, 4.9036], zoom_start=11, tiles="cartodbpositron")
map_ams_price.choropleth(
    geo_data=r'C:\neighbourhoods.geojson',
    data=df,
    columns=['neighbourhood_cleansed', 'price'],
    key_on='feature.properties.neighbourhood',
    fill_color='BuPu', 
    fill_opacity=0.7, 
    line_opacity=0.2,
    legend_name='Avg',
    reset=True,
    tooltip=folium.features.GeoJsonTooltip(fields=['neighbourhood_cleansed', 'price'],
                                                      labels=True,
                                                      sticky=False),
)

map_ams_price

在此处输入图片说明

在此处输入图片说明

Minium Example最小示例

# the price is the avg price
d = {'neighbourhood_cleansed': ['Oostelijk Havengebied - Indische Buurt', 'Centrum-Oost',
       'Centrum-West', 'Zuid', 'De Baarsjes - Oud-West', 'Bos en Lommer',
       'De Pijp - Rivierenbuurt', 'Oud-Oost', 'Noord-West', 'Westerpark',
       'Slotervaart', 'Oud-Noord', 'Watergraafsmeer',
       'IJburg - Zeeburgereiland', 'Noord-Oost', 'Buitenveldert - Zuidas',
       'Geuzenveld - Slotermeer', 'De Aker - Nieuw Sloten', 'Osdorp',
       'Bijlmer-Centrum', 'Gaasperdam - Driemond', 'Bijlmer-Oost'],
'price': [20.0,30.0,40.0,50.0,60.0,70.0,80.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,20.0]}
df = pd.DataFrame(data=d)

You can find the geojson here https://pastecode.io/s/a76fdvey你可以在这里找到 geojson https://pastecode.io/s/a76fdvey

As I mentioned in the comments, in order to give tooltips in the choropleth map, they have to be in the geojson file, not in the dataframe value, in order to be displayed.正如我在评论中提到的,为了在 choropleth 地图中提供工具提示,它们必须在 geojson 文件中,而不是在数据框值中才能显示。 So I used geopandas for the geojson file to be used, combined the data frames and added the price information to the geojson file.所以我使用 geopandas 作为要使用的 geojson 文件,组合数据框并将价格信息添加到 geojson 文件中。 The column names in the original data frame have been modified to match the geojson file.原始数据框中的列名已被修改以匹配 geojson 文件。 It can also be used as a label by adding an alias name.它也可以通过添加别名用作标签。 The tooltip can be styled, so I added that as well.工具提示可以设置样式,所以我也添加了它。

import json
import requests
import geopandas as gpd

url = "http://data.insideairbnb.com/the-netherlands/north-holland/amsterdam/2021-09-07/visualisations/neighbourhoods.geojson"

gpd_geo = gpd.read_file(url)
gpd_geo = gpd_geo.merge(df, on='neighbourhood')
geo_json_data = gpd_geo.to_json()

import folium
from folium.plugins import FastMarkerCluster
from branca.colormap import LinearColormap
from folium.features import GeoJsonPopup, GeoJsonTooltip

map_ams_price = folium.Map(location=[52.3680, 4.9036], zoom_start=11, tiles="cartodbpositron")
choropleth = folium.Choropleth(
    geo_data=geo_json_data,
    data=df,
    columns=['neighbourhood', 'price'],
    key_on='feature.properties.neighbourhood',
    fill_color='BuPu', 
    fill_opacity=0.7, 
    line_opacity=0.2,
    legend_name='Avg',
    reset=True,
    highlight=True,
).add_to(map_ams_price)

choropleth.geojson.add_child(
    folium.features.GeoJsonTooltip(fields=['neighbourhood', 'price'],
                                   aliases=['neighbourhood:', 'average_price:'],
                                   labels=True,
                                   localize=True,
                                   sticky=False,
                                   style="""
                                   background-color: #F0EFEF;
                                   border: 2px solid black;
                                   border-radius: 3px;
                                   box-shadow: 3px;
                                   """,)
)

map_ams_price

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM