使用Folium制作分级地图

3

我在制作这个区域地图时遇到了问题。我的区域是正确的,数字也是正确的,但是它填充了相同的颜色。我猜测是key_on出了问题。我的代码基于这个教程:https://blog.dominodatalab.com/creating-interactive-crime-maps-with-folium/ 代码:

[district_geo = r'C:/1/sfpddists.geojson' 
SF = (37.783087441092704, -122.46120747577555) 

crimedata2 = pd.DataFrame(df\['Police District'\].value_counts().astype(float)) 
crimedata2.to_json('crimeagg.json') 
crimedata2 = crimedata2.reset_index() 
crimedata2.columns = \['District', 'Number'\] 

m = folium.Map(location=SF, zoom_start=12) 

folium.GeoJson( 
district_geo, 
name='geojson' 
).add_to(m) 


m.choropleth(geo_data=r'C:/1/sfpddists.geojson', data=crimedata2, 
columns=\['District', 'Number'\], 
key_on=None, 
fill_color = 'PuBu', 
fill_opacity = 0.7, 
line_opacity = 0.2, 
highlight=True, 
legend_name = 'Number of incidents per district') 
m][1]
1个回答

2
key_on参数需要输入GeoJSON数据中链接到您的数字数据的字段名称。在您的代码片段中,它被设置为None,因此无法工作。在教程中,他们使用key_on = 'feature.properties.DISTRICT'
这意味着在GeoJSON数据中,每个要素都将具有名为“DISTRICT”的属性,我假设其中包含一个地区的名称。然后,在您的数据框中,您有一个名为'District'的列,其中包含与GeoJSON'DISTRICT'字段中的值匹配的字符串。当匹配时,将使用“Number”列中的值来确定颜色。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接