使用Choropleth更新输出图形时出现Plotly Dash回调错误

2024-10-04 03:22:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试更新choropleth,但这仅在我在回调之外定义图形时有效,如下所示:

fig = px.choropleth(
    df,
    geojson=counties,
    locations='COV_',
    color='Enero')

fig.update_geos(fitbounds="locations", visible=False)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})


APP.layout = html.Div([
    dbc.FormGroup(
            [
                dbc.Label("Tipo de delito"),
                dcc.Dropdown(
                    id="in-delito",
                    value=NE['Tipo de delito'].unique()[0],
                    options=[
                        {"label": col, "value": col} for col in NE['Tipo de delito'].unique()
                    ]
                ),
            ]
    ),

    dcc.Graph(id="gr-delito-map", figure=fig)

])

但当尝试使用如下回调进行更新时:

@APP.callback(
    [dash.dependencies.Output('gr-delito-map', 'figure')],
    [dash.dependencies.Input('in-delito', 'value')]
)
def update_delito(value):

    global counties

    df = pd.read_csv(BASE_PATH+'/../Descargador/data/ne.csv')
    df = df.filter(["Año", "Clave_Ent", "Tipo de delito", "Enero"])
    #df = df[df['Tipo de delito']=='Homicidio']
    df = df[df['Tipo de delito']==value]
    df[["Enero"]] = df[["Enero"]].apply(pd.to_numeric)
    df = df.groupby('Clave_Ent').sum()
    df=df.reset_index()
    df['COV_'] = df['Clave_Ent'].astype(str)

    fig = px.choropleth(
            df,
            geojson=counties,
            locations='COV_',
            color='Enero')

    return fig

从不工作,我只收到一个“回调错误更新”错误

我尝试了一段与此类似的代码,在这个链接中工作:

https://community.plot.ly/t/how-to-update-a-choropleth-map/23340/2

但不管出于什么原因,这都不适用于这个数字或数据

显示屏上的错误不会显示信息

你知道我该怎么解决这个问题吗

谢谢


Tags: inmapdfvaluefigupdatedecol