Search code examples
plotlyplotly-pythonchoropleth

Choropleth Africa map doesn’t show some countries and islands


I’m plotting an African continent map using choropleth. However, some regions are not appearing on the map, even the respective alpha codes being correct. For example, I have data from Seychelles (sov_a3="SYC"), Mauritius (sov_a3="MUS"), and the map is missing these regions or is not filled.

There is something I’m missing? I didn’t see a complete Africa map made with Plotly and I’m wondering if there is some issue with the parameter scope=“africa”

Follow below the code for the map and how it looks now.

fig_map = go.Figure(px.choropleth(df_map,
                            locations='sov_a3', color=map_count_column,
                            hover_name='country', animation_frame="date_2weeks",
                            color_continuous_scale="algae",
                            range_color=[0, max(df_map['cum_counts'])],
                            labels={'cum_counts': 'Number of genomes'}
                            ))
    fig_map.update_layout(geo_scope="africa")

Choropleth map of African continent


Solution

  • import plotly.express as px
    import plotly.graph_objects as go
    import pandas as pd
    import numpy as np
    
    countries = 'TZA,ESH,COD,SOM,KEN,SDN,TCD,ZAF,LSO,ZWE,BWA,NAM,SEN,MLI,MRT,BEN,NER,NGA,CMR,TGO,GHA,CIV,GIN,GNB,LBR,SLE,BFA,CAF,COG,GAB,GNQ,ZMB,MWI,MOZ,SWZ,AGO,BDI,MDG,GMB,TUN,DZA,ERI,MAR,EGY,LBY,ETH,DJI,UGA,RWA,SSD'
    names = "Tanzania,W. Sahara,Dem. Rep. Congo,Somalia,Kenya,Sudan,Chad,South Africa,Lesotho,Zimbabwe,Botswana,Namibia,Senegal,Mali,Mauritania,Benin,Niger,Nigeria,Cameroon,Togo,Ghana,Côte d'Ivoire,Guinea,Guinea-Bissau,Liberia,Sierra Leone,Burkina Faso,Central African Rep.,Congo,Gabon,Eq. Guinea,Zambia,Malawi,Mozambique,eSwatini,Angola,Burundi,Madagascar,Gambia,Tunisia,Algeria,Eritrea,Morocco,Egypt,Libya,Ethiopia,Djibouti,Uganda,Rwanda,S. Sudan"
    # Seychelles (sov_a3="SYC"), Mauritius (sov_a3="MUS")
    countries += ",SYC,MUS"
    names += ",Seychelles,Mauritius"
    map_count_column = "cum_counts"
    
    # simulate implied dataframe in question
    df_map = (
        pd.DataFrame({"sov_a3": countries.split(","),"country":names.split(",")})
        .merge(pd.DataFrame({"date_2weeks": list("ABC")}), how="cross")
        .assign(
            cum_counts=lambda d: np.random.randint(1, 5, len(d)),
        )
    )
    
    # go.Figure is spurious remove
    fig_map = px.choropleth(
        df_map,
        locations="sov_a3",
        color=map_count_column,
        hover_name="country",
        animation_frame="date_2weeks",
        color_continuous_scale="algae",
        range_color=[0, max(df_map["cum_counts"])],
        labels={"cum_counts": "Number of genomes"},
    )
    fig_map.update_layout(geo_scope="africa")
    
    
    • now I use a different geojson where the islands are defined. However this is imperfect as I have not ensured names map correctly to geojson
    • this does show Mauritius
    import requests
    res = requests.get("https://raw.githubusercontent.com/codeforgermany/click_that_hood/main/public/data/africa.geojson")
    
    # use out own geometry
    fig_map = px.choropleth(
        df_map,
        geojson=res.json(),
        locations="country",
        featureidkey="properties.name",
        color=map_count_column,
        hover_name="country",
        animation_frame="date_2weeks",
        color_continuous_scale="algae",
        range_color=[0, max(df_map["cum_counts"])],
        labels={"cum_counts": "Number of genomes"},
    )
    
    fig_map.update_geos(fitbounds="locations")
    

    enter image description here