首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Choropleth地图的Dash应用程序回调

Choropleth地图的Dash应用程序回调
EN

Stack Overflow用户
提问于 2022-09-26 15:11:16
回答 1查看 81关注 0票数 0

我有一些问题,回调函数在巧妙的破折号。我希望我的合唱地图能根据选定的年份更新。滑块本身工作,并选择正确的数据(使用"print(filtered_df)“语句检查)。然而,地图并没有相应地更新,我只是不明白为什么。我遗漏了什么?

谢谢你的帮助!

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2022-09-29 05:17:49

..。发现问题了!我只是没有在代码的这一部分调用正确的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)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73856212

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档