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