Folium: colors not displaying on choropleth map

Created on 18 Jul 2018  Â·  2Comments  Â·  Source: python-visualization/folium

Please add a code sample or a nbviewer link, copy-pastable if possible

import pandas as pd
sf_crime = pd.read_csv('https://cocl.us/sanfran_crime_dataset')
sf_crime.drop(['IncidntNum', 'Category', 'Descript', 'DayOfWeek', 'Date', 'Time', 'Resolution', 'Address', 'X', 'Y', 'Location', 'PdId'], axis=1, inplace=True)
by_zone = sf_crime.apply(pd.Series.value_counts).reset_index()
by_zone.rename(columns={'index':'Neighborhood', 'PdDistrict':'Count'}, inplace=True)

!wget --quiet https://cocl.us/sanfran_geojson -O sf_neighborhoods.json
sf_zones = r'sf_neighborhoods.json'
!pip install folium==0.5.0

sf_lat = 37.77
sf_lon = -122.42
sf_crime_map = folium.Map(location=[sf_lat, sf_lon], 
                         zoom_start=12, 
                         tiles='Mapbox Bright')

sf_crime_map.choropleth(
    geo_data=sf_zones,
    data=by_zone,
    columns=['Neighborhood', 'Count'],
    key_on='feature.properties.name',
    fill_color='YlOrRd', 
    fill_opacity=0.7, 
    line_opacity=0.2,
    legend_name='San Fransisco Crime by Neighborhood'
)

sf_crime_map

Problem description

The above code is used in a Jupyter notebook, and generates a map, but the map doesn't not show the any colors corresponding to the number of crimes in each SF neighborhood.

Expected Output

Output of folium.__version__

A map

question

Most helpful comment

The value you're passing to the key_on argument is not correct. It should refer to the field in your geojson data to which the data should be bound. There actually is no 'name' field in your geojson. Take a look in the sanfran_geojson file to see. Since you're using the 'Neighborhood' column in your data, I assume you want to bind to the 'DISTRICT' field in the geojson. So use 'feature.properties.DISTRICT'. Hope that helps.

All 2 comments

The value you're passing to the key_on argument is not correct. It should refer to the field in your geojson data to which the data should be bound. There actually is no 'name' field in your geojson. Take a look in the sanfran_geojson file to see. Since you're using the 'Neighborhood' column in your data, I assume you want to bind to the 'DISTRICT' field in the geojson. So use 'feature.properties.DISTRICT'. Hope that helps.

Absolutely, thanks Frank! I actually realized that, and tried changing name
to DISTRICT but didn't work on first run, it seems to be working fine now
though. Thanks for the clarification Frank!

El mié., 18 de jul. de 2018 a la(s) 03:27, Frank ([email protected])
escribió:

The value you're passing to the key_on argument is not correct. It should
refer to the field in your geojson data to which the data should be bound.
There actually is no 'name' field in your geojson. Take a look in the
sanfran_geojson file to see. Since you're using the 'Neighborhood' column
in your data, I assume you want to bind to the 'DISTRICT' field in the
geojson. So use 'feature.properties.DISTRICT'. Hope that helps.

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/python-visualization/folium/issues/913#issuecomment-405852276,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AJ49EcboRI_J6ykI9ptEvDiPOSdtkjNQks5uHvF9gaJpZM4VTwRR
.

Was this page helpful?
0 / 5 - 0 ratings