3

我在制作这个 choroepth 地图时遇到了问题。这些区域是正确的,我得到了正确的数字,但它用相同的颜色填充了我的区域。我的假设是我弄错了 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]
4

1 回答 1

2

key_on参数需要 GeoJSON 数据中链接到您的数值数据的字段的名称。在您的代码片段中,它设置为None,因此它不起作用。在他们使用的教程中key_on = 'feature.properties.DISTRICT'

这意味着在 GeoJSON 数据中,每个要素都有一个名为“DISTRICT”的属性,我假设该属性将包含一个地区的名称。然后,在您的数据框中,您有一个名为 'District' 的列,其中包含与 GeoJSON 'DISTRICT' 字段中的值匹配的字符串。如果匹配,“数字”列中的值将用于确定颜色。

于 2019-01-13T20:54:11.940 回答