Plotly Dash App回调Choropleth地图



我对plotly的短划线中的回调函数有一些问题。我希望我的choropleth地图根据所选年份进行更新。滑块本身确实工作并且选择了正确的数据(用"print(filtered_df("进行检查;声明(。然而,地图并没有相应地更新,我只是不明白为什么。我错过了什么?

谢谢你的帮助!

app = dash.Dash(__name__, external_stylesheets=[dbc.themes.FLATLY])
app.layout = dbc.Container(html.Div([
dbc.Row(
[
dcc.Graph(id='graph-with-slider'),
dcc.Slider(
min=df['year'].min(),
max=df['year'].max(),
step=None,
value=df['year'].max(),
marks={str(year): str(year) for year in df['year'].unique()},
id='year-slider'
)
]
)
]))

@app.callback(
Output(component_id='graph-with-slider', component_property='figure'),
[Input(component_id='year-slider', component_property='value')]
)
def update_figure(selected_year):
filtered_df = df.loc[df.year == selected_year].reset_index()
#print(filtered_df)
fig = px.choropleth(filtered_df, locations=df['Country Code'], locationmode='ISO-3',
color=df['life_expectancy_total'], color_continuous_scale=colorscale)
fig.update_layout(transition_duration=500)
return fig
if __name__ == '__main__':
app.run_server(debug=True)

。。。发现问题了!我只是在这部分代码中没有调用正确的df:/

fig = px.choropleth(filtered_df, locations=df['Country Code'], locationmode='ISO-3', color=df['life_expectancy_total'], color_continuous_scale=colorscale)

应该是:

fig = px.choropleth(filtered_df, locations=filtered_df['Country Code'], locationmode='ISO-3', color=filtered_df.life_expectancy_avg, color_continuous_scale=colorscale)

最新更新