I have a problem making this choroepth map. The areas are right, i got the numbers correct, but it fills my areas with the same color. My assumption is that I am getting the key_on wrong. My code is based on this tutorial: https://blog.dominodatalab.com/creating-interactive-crime-maps-with-folium/ Code:
[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]
The key_on argument expects the name of the field in the GeoJSON data that links to your numerical data. In your code snippet it is set to None, so it doesn't work. In the tutorial they use key_on = 'feature.properties.DISTRICT'.
This means that in the GeoJSON data each feature will have a property named 'DISTRICT' which I assume will contain the name of a district. Then, in your dataframe you have a column named 'District' with strings that match the value in the GeoJSON 'DISTRICT' field. Where there is a match the value in the 'Number' column will be used to determine the color.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With