Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly use key_on in folium choropleths

I am attempting to make a choropleth map of San Francisco Crime. I have the map all set and everything is working properly except for the colors in the polygons. They are all stuck at the lightest color. I suspect I am not using the key_on parameter correctly.

I have tried

 key_on = 'feature.properties.district'

as this is how the poylgons are lableled in json file. I have also tried listing the neighborhoods directly.

dfU = 

Neighborhood    Count
0   BAYVIEW 14303
1   CENTRAL 17666
2   INGLESIDE   11594
3   MISSION 19503
4   NORTHERN    20100
5   PARK    8699
6   RICHMOND    8922
7   SOUTHERN    28445
8   TARAVAL 11325
9   TENDERLOIN  9942

sf_map = folium.Map(location=[37.7749,-122.4194],
                    zoom_start = 12,
                    )
bins = [8699, 12648, 16597, 20546, 24495, 28445]

sf_map.choropleth(
    geo_data=sf_geo,
    data=dfU,
    columns = ['Neighborhood','Count'],
    key_on = 'feature.properties.district',
    threshold_scale = bins,
    fill_color='YlOrRd', 
    fill_opacity=0.7, 
    line_opacity=0.2,
    legend_name='SF Crime'
)
sf_map

The expected out some is a map of SF with polygons shaded darker where the total number of crimes (count) is higher based on the bins I have defined.

like image 288
yousof aly Avatar asked Sep 16 '25 11:09

yousof aly


1 Answers

Is case sensitive, and the property is called "DISTRICT". It should work after you make that change. It worked for me.

key_on = 'feature.properties.DISTRICT', 
like image 137
jmpotero Avatar answered Sep 19 '25 00:09

jmpotero