You need to enable JavaScript to run this app. Plotly/Dash单页双图关联同一下拉菜单出现空白饼图问题求助
优惠活动
大模型
产品
解决方案
定价
更多

Plotly/Dash单页双图关联同一下拉菜单出现空白饼图问题求助

问题定位

你遇到的空白图表问题和回调绑定逻辑无关,是代码中存在基础的变量名不匹配错误,同时存在命名不规范的潜在问题:

  • 第一个回调函数内你将饼图对象赋值给了fig变量,但最终返回的是未定义的fig_1,返回值为空导致图表无内容
  • 第二个回调函数同理,你将饼图对象赋值给了fig变量,最终返回的是未定义的fig_2
  • 额外建议:不要使用Python内置关键字all作为自定义变量名,避免引发不必要的命名冲突,可更换为all_pillars这类自定义名称
  • 请提前确认你已经正确导入所有依赖包(dash、dash.html、dash.dcc、plotly.express、pandas),且数据集df的字段Pillar、Cadence、Project No的名称、大小写完全和代码中一致,无拼写错误
修正后可运行代码
# 请先确保你已经导入所有依赖,示例导入语句如下:
# import dash
# from dash import html, dcc, Input, Output
# import plotly.express as px
# import pandas as pd

# 请提前正确加载你的数据集df
app = dash.Dash(__name__)

all_pillars = df.Pillar.unique()

app.layout=html.Div(className='row', children=[
    html.H1("PRO Project Management dashboard"),
    dcc.Dropdown(id='pillar-choice',
                 options=[{'label':x, 'value':x} for x in all_pillars],
                 value=all_pillars,
                 multi=True),
    html.Div(children=[
    dcc.Graph(id='graph1', style={'display':'inline-block', 'width': '50%'}),
    dcc.Graph(id='graph2', style={'display':'inline-block', 'width': '50%'})
    ])
])

@app.callback(
    Output(component_id='graph1', component_property='figure'),
    Input(component_id='pillar-choice', component_property='value')
)
def update_graph1(value_pillar):
        print(value_pillar)
        dff = df[df.Pillar.isin(value_pillar)]
        fig = px.pie(data_frame=dff, names='Pillar', values='Project No', title='Number of Projects by Pillars')
        return fig
    
@app.callback(
    Output(component_id='graph2', component_property='figure'),
    Input(component_id='pillar-choice', component_property='value')
)
def update_graph_2(value_pillar):
        print(value_pillar)
        dff = df[df.Pillar.isin(value_pillar)]
        fig = px.pie(data_frame=dff, names='Cadence', values='Project No', title='Number of Projects by Cadence')
        return fig
    
if __name__=='__main__':
    app.run_server()

内容的提问来源于stack exchange,提问作者Sheryna

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.11 02:48:03