使用破折号创建仪表板时发生回调错误

时间:2019-11-19 15:41:03

标签: python pandas dashboard plotly-dash

我正在尝试使用破折号构建交互式仪表板。我想显示一个散点图,每个图代表一个战斗机,y轴代表赢得的战斗次数,x轴代表战斗的战斗次数。

我想允许用户使用多下拉箭头选择不同的部门(选择一个或多个部门)。

在一些在线帮助下,这是我想出的:

data['bouts_fought'] = data['w'].astype('float')+data['l'].astype('float')+data['d'].astype('float')
WEIGHT_CLASS = data['division'].unique()
app = dash.Dash()
app.css.append_css({
    "external_url": "https://codepen.io/chriddyp/pen/bWLwgP.css"
})
# layout
app.layout = html.Div(children=[
    html.H1(children='Visualizing boxer stats', style={
        'textAlign': 'center',
    }),
    dcc.Dropdown(
        id='weight_class',
        options=[{'label': i, 'value': i} for i in data['division'].unique()],
        multi=True
    ),
    dcc.Graph(
        id='total-bouts-v-bouts-won',
    )
])
@app.callback(
    dash.dependencies.Output('total-bouts-v-bouts-won', 'figure'),
    [dash.dependencies.Input('weight_class', 'value')])
def update_scatterplot(weight_class):
    if weight_class is None or weight_class == []:
        weight_class = WEIGHT_CLASS

    weight_df = data[(data['division'].isin(weight_class))]
    return {
        'data': [
            go.Scatter(
                x=weight_df['bouts_fought'],
                y=weight_df['w'],
                text=weight_df['name'],
                mode='markers',
                opacity=0.5,
                marker={
                    'size': 14,
                    'line': {'width': 0.5, 'color': 'blue'}
                },
            )
        ],
        'layout': go.Layout(
            xaxis={'title': 'Bouts fought'},
            yaxis={'1': 40, 'b': '40', 't': 10, 'r': 10},
            legend={'x': 0, 'y': 1},
            hovermode='closest'
        )
    }
if __name__ == '__main__':
    app.run_server(debug=True)

但是,当我尝试运行该应用程序时,会收到以下警告消息:

  下拉列表中ID为“ weight_class”的

options [14] .label是必需的,但   没有提供。回调错误更新

     

合计bouts-v-bouts-won.figure ValueError:无效的属性   为plotly.graph_objs.layout.YAxis类型的对象指定:('1',   'b','t','r')

以下是我的数据示例:

{'name': {0: 'Roberto Salas',
  3: 'James Jackson',
  6: 'Alex Love',
  9: 'Juan Centeno',
  12: 'Jordan Weeks'},
 'division': {0: 'cruiser',
  3: 'heavy',
  6: 'bantam',
  9: 'fly',
  12: 'super middle'},
 'w': {0: 5.0, 3: 4.0, 6: 3.0, 9: 4.0, 12: 2.0},
 'l': {0: 0.0, 3: 0.0, 6: 0.0, 9: 3.0, 12: 0.0},
 'd': {0: 0.0, 3: 1.0, 6: 0.0, 9: 1.0, 12: 0.0},
 'location': {0: 'USA', 3: 'USA', 6: 'USA', 9: 'USA', 12: 'USA'},
 'from': {0: 2016.0, 3: 2017.0, 6: 2018.0, 9: 2016.0, 12: 2019.0},
 'sex': {0: 'male', 3: 'male', 6: 'female', 9: 'male', 12: 'male'}}

我根据给出的答案更新了代码,但仍然收到此错误消息:

Error: options[14].label in Dropdown with ID "weight_class" is required but it was not provided.

    at propTypeErrorHandler (http://127.0.0.1:8050/_dash-component-suites/dash_renderer/dash_renderer.v1_2_0m1574163797.dev.js:33569:9)

    at CheckedComponent (http://127.0.0.1:8050/_dash-component-suites/dash_renderer/dash_renderer.v1_2_0m1574163797.dev.js:30047:77)

    at renderWithHooks (http://127.0.0.1:8050/_dash-component-suites/dash_renderer/react-dom@16.v1_2_0m1574163797.8.6.js:13073:18)

    at mountIndeterminateComponent (http://127.0.0.1:8050/_dash-component-suites/dash_renderer/react-dom@16.v1_2_0m1574163797.8.6.js:15155:13)

    at beginWork (http://127.0.0.1:8050/_dash-component-suites/dash_renderer/react-dom@16.v1_2_0m1574163797.8.6.js:15760:16)

    at performUnitOfWork (http://127.0.0.1:8050/_dash-component-suites/dash_renderer/react-dom@16.v1_2_0m1574163797.8.6.js:19447:12)

    at workLoop (http://127.0.0.1:8050/_dash-component-suites/dash_renderer/react-dom@16.v1_2_0m1574163797.8.6.js:19487:24)

    at renderRoot (http://127.0.0.1:8050/_dash-component-suites/dash_renderer/react-dom@16.v1_2_0m1574163797.8.6.js:19570:7)

    at performWorkOnRoot (http://127.0.0.1:8050/_dash-component-suites/dash_renderer/react-dom@16.v1_2_0m1574163797.8.6.js:20477:7)

    at performWork (http://127.0.0.1:8050/_dash-component-suites/dash_renderer/react-dom@16.v1_2_0m1574163797.8.6.js:20389:7)

1 个答案:

答案 0 :(得分:0)

错误

  

总折断-v-折断-wo​​n.figure ValueError:为plotly.graph_objs.layout.YAxis类型的对象指定了无效的属性:('1','b','t','r')

是因为您的布局定义:

    'layout': go.Layout(
        xaxis={'title': 'Bouts fought'},
        yaxis={'1': 40, 'b': '40', 't': 10, 'r': 10},
        legend={'x': 0, 'y': 1},
        hovermode='closest'
    )

不确定yaxis={'1': 40, 'b': '40', 't': 10, 'r': 10}的用途,但是1btr无效。您可以使用yaxis查看print(go.Layout.yaxis.__doc__)词典的有效属性的完整列表。

如果我从布局字典中删除了yaxis,则该应用程序将以您的示例数据正常运行,并且在下拉菜单中选择项目时图形也会更新。我在任何时候都没有看到第一个错误(Dropdown中的options [14] .label ...)。