Search code examples
python-3.xfoliumchoropleth

Why does Python Choropleth map not display colors?


I would like to create a Choropleth map in Python using Folium library.

This is the dataset:

df_new_count2

Reviewer Country Count Original Number Percentage

0 Greece 191 3352 0.056981

1 Belgium 338 5991 0.056418

2 Germany 429 7843 0.054698

The Json file used is the following: https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DV0101EN/labs/Data_Files/world_countries.json

This is the code I used:

world_map = folium.Map(location=[40, 0], zoom_start=1.5)

folium.Choropleth( geo_data='https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DV0101EN/labs/Data_Files/world_countries.json',

    name='choropleth',

    data=df_new_count2,

    columns=['Reviewer Country','Percentage'],

    key_on='feature.properties.name',

    fill_color='YlOrRd',

    fill_opacity=0.7,

    line_opacity=0.2,

    legend_name='Percentage of people'
).add_to(world_map)

folium.LayerControl().add_to(world_map)

world_map

The map is displaying but everything is black, the colors are not showing.

Map

Does anyone know what the issue might be? Thanks in advance for your time.


Solution

  • You can try:

    import pandas as pd
    import folium
    
    # create dataframe
    df_new_count2 = pd.DataFrame([['Greece', 0.056981], ['Belgium', 0.056418], ['Germany', 0.054698]], columns=['Reviewer Country','Percentage'])
    df_new_count2
    
    # plot map
    world_map = folium.Map(location=[40, 0], zoom_start=1.5)
    folium.Choropleth(geo_data='https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DV0101EN/labs/Data_Files/world_countries.json', name='choropleth',
        data=df_new_count2,
        columns=['Reviewer Country','Percentage'],
        key_on='feature.properties.name',
        fill_color='YlOrRd',
        fill_opacity=0.7,
        line_opacity=0.2,
        legend_name='Percentage of people'
    ).add_to(world_map)
    folium.LayerControl().add_to(world_map)
    world_map
    

    Result: enter image description here

    It only colors the countries for which data is available in the dataframe. Can you validate your dataframe contains data for the countries selected?