我有两个choropleth层,我想在其中添加geojsontoltip,但是我一直收到错误TypeError: __init__() missing 1 required positional argument: 'text'
我目前的代码如下。在
import folium
import pandas as pd
import json
df_theft = pd.read_csv('PA_Theft.csv')
df_assualt = pd.read_csv('PA_Assualt.csv')
theft_json = json.load(open('theft_geojson.json'))
assualt_json = json.load(open('assualt_geojson.json'))
m = folium.Map(location=[41.20, -77.50], tiles="cartodbdark_matter", zoom_start=8.3)
theft = folium.Choropleth(
geo_data=theft_json,
data=df_theft,
columns=['county_name', 'rate'],
key_on='feature.properties.county_name',
fill_color='OrRd',
fill_opacity=0.9,
nan_fill_color='#ffffff',
nan_fill_opacity=0.9,
legend_name='Incident rate per 100,000 people',
highlight=True,
name='Theft'
).add_to(m)
folium.GeoJson(
theft_json,
tooltip=folium.features.Tooltip(fields=['feature.properties.county_name'])
).add_to(theft)
assualt = folium.Choropleth(
geo_data=assualt_json,
data=df_assualt,
columns=['county_name', 'rate'],
key_on='feature.properties.county_name',
fill_color='OrRd',
fill_opacity=0.9,
nan_fill_color='#ffffff',
nan_fill_opacity=0.9,
legend_name='Incident rate per 100,000 people',
highlight=True,
name='Assualt'
).add_to(m)
folium.GeoJson(
assualt_json,
tooltip=folium.features.Tooltip(fields=['feature.properties.county_name'])
).add_to(assualt)
folium.LayerControl().add_to(m)
m.save('Crime_Map.html')
print('Map created.')
我要寻找的最终结果是,当用户将鼠标悬停在PA中的每个县上时,工具提示弹出窗口被激活,并显示来自geoJSON的以下信息。在
geojson示例
^{pr2}$
两件事:
GeoJsonTooltip
类来使用geojson字段。常规的Tooltip
类只能处理简单文本。这就是你得到的错误。在GeoJsonTooltip
传递给GeoJson
对象,该对象由Choropleth
在幕后创建:GeoJsonTooltip(....).add_to(theft.geojson)
相关问题 更多 >
编程相关推荐