0

I'm trying to make a choropleth map of country Indonesia to visualize the population value of each provinces. I'm using the plotly graph_objects module. The legend is showing, but the map just show as blank white. How can I solve this problem?

This is my code:

import requests
import pandas as pd
import plotly.graph_objects as go

# Load data
df_reshaped = pd.read_csv('D:/Projects/population-dashboard-master/data/id-population-2014-2022-reshaped.csv')

# Sidebar
with st.sidebar:
st.title('󠁧󠁢󠁥Indonesia Population Dashboard')
selected_year = st.selectbox('Select a year', year_list)
df_selected_year = df_reshaped[df_reshaped.year == selected_year]
color_theme_list = ['blues', 'cividis', 'greens', 'inferno', 'magma', 'plasma', 'reds', 'rainbow', 'turbo', 'viridis']
selected_color_theme = st.selectbox('Select a color theme', color_theme_list)

# Choropleth map

def make_choropleth(input_df, input_id, input_column, input_color_theme):
    # Get Indonesia geojson data
    geojson = requests.get(
        "https://raw.githubusercontent.com/superpikar/indonesia-geojson/master/indonesia-province-simple.json"
    ).json()

    # Plot choropleth map

    choropleth_map = go.Figure(
        data=go.Choropleth(
            geojson=geojson,
            locations=input_df[input_id],  # Spatial coordinates
            featureidkey="properties.Propinsi",
            z=input_df[input_column],  # Data to be color-coded
            colorscale=input_color_theme,
            colorbar_title=input_id,
        )
    )
    choropleth_map.update_geos(fitbounds="locations", visible=False)

    return choropleth_map

# Calling the function
choropleth = make_choropleth(df_selected_year, 'province', 'population', selected_color_theme)
st.plotly_chart(choropleth, use_container_width=True)

and here is the code reference that I use:

import requests
import pandas as pd
import plotly.graph_objects as go

# indonesia geojson
geojson = requests.get(
    "https://raw.githubusercontent.com/superpikar/indonesia-geojson/master/indonesia-province-simple.json"
).json()

# dataframe with columns referenced in question
df = pd.DataFrame(
    {"Column": pd.json_normalize(geojson["features"])["properties.Propinsi"]}
).assign(Columnnext=lambda d: d["Column"].str.len())

fig8 = go.Figure(
    data=go.Choropleth(
        geojson=geojson,
        locations=df["Column"],  # Spatial coordinates
        featureidkey="properties.Propinsi",
        z=df["Columnnext"],  # Data to be color-coded
        colorscale="Reds",
        colorbar_title="Column",
    )
)
fig8.update_geos(fitbounds="locations", visible=False)

fig8
4
  • The population data used in the map is not in your question, so I got the data from here and converted 3 years to long format and applied your code. Normal switching of years and color maps is working correctly. Commented Apr 10, 2024 at 13:19
  • The output is this. Commented Apr 10, 2024 at 13:20
  • @r-beginners may I see your code? It still doesn't work on my end. Commented Apr 10, 2024 at 15:48
  • @r-beginners Hi. I found the issue already, thank you very much! Commented Apr 11, 2024 at 17:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.