Search code examples
pythonpandasgeojsonchoroplethfolium

Choropleth map is not showing color variation in the output


I am not getting any color variation in my output map even after the choropleth is linked with the geo_data and the data frame is linked with data parameter in the choropleth method.

I have provided the "key_on" parameter rightly and the "columns" parameter rightly. I have removed all the NULL values from the Dataframe.

import pandas as pd
from pandas import read_csv
import folium
import os
import webbrowser

crimes = read_csv('Dataframe.csv',error_bad_lines=False)

vis = os.path.join('Community_Areas.geojson')
m = folium.Map(location = [41.878113, -87.629799], zoom_start = 10, tiles = "cartodbpositron")
m.choropleth(geo_data=vis, data = crimes, columns = ['Community Area', 'count'], fill_color = 'YlGn', key_on = 'feature.properties.area_numbe')
folium.LayerControl().add_to(m)
m.save('map.html')
webbrowser.open(filepath)

I expected a colored choropleth map, but the actual output was completely grey. I will add the code, data, output in the link below.

Code link: https://github.com/rahul0070/Stackoverflow_question_data

Data

,Community Area,count
0,25.0,92679
1,8.0,48751
2,43.0,47731
3,23.0,45943
4,29.0,44819
5,28.0,42243
6,71.0,40624
7,67.0,40157
8,24.0,39680
9,32.0,38513
10,49.0,37227
11,68.0,37023
12,69.0,35874
13,66.0,33877
14,44.0,32256
15,6.0,31043
16,26.0,30565
17,27.0,28113
18,61.0,27362
19,22.0,27329
20,46.0,26897
21,19.0,26198
22,30.0,24362
23,53.0,22645
24,42.0,21284
25,7.0,21047
26,1.0,19932
27,3.0,19799
28,15.0,17820
29,38.0,17660
30,2.0,17213
31,73.0,17071
32,16.0,15926
33,40.0,14943
34,58.0,14143
35,31.0,13934
36,63.0,13203
37,70.0,12980
38,35.0,12965
39,14.0,12714
40,77.0,12612
41,21.0,12587
42,75.0,11156
43,65.0,10812
44,51.0,10348
45,56.0,10176
46,4.0,9984
47,33.0,8987
48,60.0,8982
49,76.0,8938
50,20.0,8922
51,17.0,8536
52,41.0,8076
53,48.0,7823
54,5.0,7761
55,45.0,7485
56,39.0,7466
57,52.0,7150
58,54.0,6777
59,10.0,6372
60,11.0,6072
61,34.0,6053
62,62.0,5736
63,50.0,5730
64,59.0,5695
65,57.0,5244
66,64.0,5194
67,72.0,4962
68,37.0,4908
69,13.0,4570
70,36.0,3359
71,74.0,3145
72,55.0,3109
73,18.0,3109
74,12.0,2454
75,47.0,2144
76,9.0,1386

Solution

  • After reading the data what I found was you did not convert the data type of Community Area column to string type as the geojson file contains the key in the form of string. So the data type of both the key and column should match.

    Here is a full fledged solution to your answer

    # importing libraries
    import pandas as pd
    from pandas import read_csv
    import folium
    import os
    import webbrowser
    # read the data
    crimes = read_csv('Dataframe.csv',error_bad_lines=False)
    # convert float to int then to string
    crimes['Community Area'] = crimes['Community Area'].astype('int').astype('str')
    # choropleth map
    vis = 'Community_Areas.geojson'
    m = folium.Map(location = [41.878113, -87.629799], zoom_start = 10, tiles = "cartodbpositron")
    m.choropleth(geo_data=vis, data = crimes, columns = ['Community Area', 'count'], fill_color = 'YlGn', key_on = 'feature.properties.area_num_1')
    folium.LayerControl().add_to(m)
    m.save('map.html')
    webbrowser.open('map.html')
    

    Please comment if you find difficulty in understanding any part of code. For the key_on parameter you can also try feature.properties.area_numbe