G. *_*cia 8 python python-3.x leaflet choropleth folium
Folium 允许使用工具提示或弹出文本创建标记。我想对我的 GeoJSON 多边形做同样的事情。
我的 GeoJSON 有一个名为"name"(feature.properties.name-> 假设它是美国每个州的名称)的属性。除了每个州的失业率之外,我希望能够将其显示为我的分区统计图中的标签。"State"我在的专栏中也有相同的信息pandas dataframe。
这可能吗?我很高兴有一个解决方案,允许它成为一个弹出窗口、工具提示或写在上面的简单文本标签。
import pandas as pd
url = (
"https://raw.githubusercontent.com/python-visualization/folium/master/examples/data"
)
state_geo = f"{url}/us-states.json"
state_unemployment = f"{url}/US_Unemployment_Oct2012.csv"
state_data = pd.read_csv(state_unemployment)
m = folium.Map(location=[48, -102], zoom_start=3)
folium.Choropleth(
geo_data=state_geo,
name="choropleth",
data=state_data,
columns=["State", "Unemployment"],
key_on="feature.id",
fill_color="YlGn",
fill_opacity=0.7,
line_opacity=0.2,
legend_name="Unemployment Rate (%)",
).add_to(m)
folium.LayerControl().add_to(m)
m
Run Code Online (Sandbox Code Playgroud)
Bob*_*ner 15
过去我必须使用 folium 的 GeoJsonTooltip() 和其他一些步骤来完成此任务。我很想知道是否有人有更好的方法
url = (
"https://raw.githubusercontent.com/python-visualization/folium/master/examples/data"
)
state_geo = f"{url}/us-states.json"
state_unemployment = f"{url}/US_Unemployment_Oct2012.csv"
state_data = pd.read_csv(state_unemployment)
m = folium.Map(location=[48, -102], zoom_start=3)
# capturing the return of folium.Choropleth()
cp = folium.Choropleth(
geo_data=state_geo,
name="choropleth",
data=state_data,
columns=["State", "Unemployment"],
key_on="feature.id",
fill_color="YlGn",
fill_opacity=0.7,
line_opacity=0.2,
legend_name="Unemployment Rate (%)",
).add_to(m)
# creating a state indexed version of the dataframe so we can lookup values
state_data_indexed = state_data.set_index('State')
# looping thru the geojson object and adding a new property(unemployment)
# and assigning a value from our dataframe
for s in cp.geojson.data['features']:
s['properties']['unemployment'] = state_data_indexed.loc[s['id'], 'Unemployment']
# and finally adding a tooltip/hover to the choropleth's geojson
folium.GeoJsonTooltip(['name', 'unemployment']).add_to(cp.geojson)
folium.LayerControl().add_to(m)
m
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10157 次 |
| 最近记录: |