Search code examples
pythonplotlyvisualization

Plotly colors the entire world map of a single color


I tried to create a corographic map about the sismicity level in Sicily using a GeoJSON file with plotly module (https://raw.githubusercontent.com/Gangelo99/Sismicita-Sicilia/main/classificazione-sismica.geojson). But when I try to show the corographic map, all world is with single color. What I'm supposed to do?

from urllib.request import urlopen
import json
import pandas as pd
import plotly.express as px

with urlopen('https://raw.githubusercontent.com/Gangelo99/Sismicita-Sicilia/main/classificazione-sismica.geojson') as response:
    polygons = json.load(response)


df = pd.read_csv("dataset_choropleth.csv", dtype={"comune": str})

fig = px.choropleth(
    df,
    geojson=polygons,
    locations='comune',
    # locationmode='country names',
    featureidkey='properties.Comune',
    color="classificazione",
    color_continuous_scale="Viridis",
    range_color=(1, 4),
    # scope="europe",
    labels={"aaa"},
)
fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0})
fig.update_geos(fitbounds="locations", visible=True)
fig.show()

dataset_choropleth.csv

This is the result: enter image description here

I tried to set the featureidkey='properties.Comune', but It's seems to not work


Solution

  • The same event was confirmed, so I looked for another geojson file and replaced the geojson data. After replacing the geojson file, the map was displayed correctly. Perhaps there was a problem with the geojson file, because the map outline provided by px.choropleth() is simplified, so it is better to use choropleth_mapbox() for this type of map to make the map look better.

    from urllib.request import urlopen
    import json
    import pandas as pd
    import plotly.express as px
    
    with open('./data/limits_R_19_municipalities.geojson', encoding='utf-8') as response:
        polygons = json.load(response)
    
    url = 'https://raw.githubusercontent.com/Gangelo99/Sismicita-Sicilia/main/dataset_choropleth.csv'
    df = pd.read_csv(url, index_col=0, dtype={"comune": str})
    
    fig = px.choropleth(
        df,
        geojson=polygons,
        locations='comune',
        featureidkey='properties.name',
        color="classificazione",
        color_continuous_scale="Viridis",
        range_color=(1, 4),
        labels={"aaa"},
    )
    fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0})
    fig.update_geos(fitbounds="locations", visible=True)
    fig.show()
    

    enter image description here

    px.choropleth_mapbox()

    fig = px.choropleth_mapbox(
        df,
        geojson=polygons,
        locations='comune',
        featureidkey='properties.name',
        color="classificazione",
        color_continuous_scale="Viridis",
        mapbox_style="carto-positron",
        zoom=6,
        center={'lat': 37.50000,'lon': 15.090278},
        range_color=(1, 4),
        labels={"aaa"},
    )
    

    enter image description here