返回 Dash 中的图形

Returning figure in Dash

我有以下代码可以绘制图表。但是,它会变成空白。

@app.callback(Output('third', 'figure'),
             [Input('country_drop', 'value')])

def summary_combined(country):

    df = infection_type.groupby(['country', 'date', 'type']).sum()
    df.reset_index(inplace = True)
    df = qq[qq['country'] == country]
    df['confirmed'] = df[df['type'] == 'confirmed']['cases'].cumsum()
    df['deaths'] = df[df['type'] == 'death']['cases'].cumsum()
    df['recovered'] = df[df['type'] == 'recovered']['cases'].cumsum()
    df = df.fillna(0)
    df['total'] = df['confirmed'] + df['deaths'] + df['recovered']
    cdf = df[df['type'] == 'confirmed']
    ddf = df[df['type'] == 'death']
    rdf = df[df['type'] == 'recovered']

    fig = go.Figure()

    fig.add_trace(go.Scatter(x = cdf['date'],
                            y = cdf['total'],
                            line=dict(color='royalblue', width=2),
                            name = 'Confirmed'))

    fig.add_trace(go.Scatter(x = ddf['date'],
                            y = ddf['total'],
                            line=dict(color='firebrick', width=2),
                            name = 'Deaths'))

    fig.add_trace(go.Scatter(x = rdf['date'],
                            y = rdf['total'],
                            line=dict(color='green', width=2),
                            name = 'Recovered'))
    #fig = {'data' : traces, 'layout': {'title':stock_ticker}}

    return fig

相同的 html 组件代码是:

app.layout = html.Div([

                        dcc.Dropdown(id = 'country_drop',
                                    options = country_dropdown,
                                    value = 'India'),
                        dcc.Graph(id = 'first'),


                        html.Div([
                                dcc.Graph(id = 'second')
                        ]),

                        html.Div([
                                dcc.Graph(id = 'third')
                        ])
])

这是我第一次尝试在 Dash 中使用 add_trace 绘制折线图。我使用

绘制了其他图表
return {'data' : traces, 'layout': go.Layout(title = 'Cases per day: {cc}'.format(cc = country) , xaxis = {'title' : 'Date'},
                              yaxis = {'title': '#'})}

或类似的东西。但这不适用于此代码。请指导我。谢谢!

这是该应用程序的完整代码:

country_dropdown = []
for c in df['country'].unique():
    country_dropdown.append({'label':str(c), 'value':str(c)})

app = dash.Dash()

app.layout = html.Div([

                        dcc.Dropdown(id = 'country_drop',
                                    options = country_dropdown,
                                    value = 'India'),
                        dcc.Graph(id = 'first'),


                        html.Div([
                                dcc.Graph(id = 'second')
                        ]),

                        html.Div([
                                dcc.Graph(id = 'third')
                        ])
])

#first graph: cases per day
@app.callback(Output('first', 'figure'),
             [Input('country_drop', 'value')])
def summary_cases(country):

    df = date_country[date_country['country'] == country].copy()    
    trace1 = [go.Bar(
                    x = df['date'],
                    y = df['cases'])]


    return {'data' : trace1, 'layout': go.Layout(title = 'Cases per day: {cc}'.format(cc = country) , xaxis = {'title' : 'Date'},
                              yaxis = {'title': '#'})}

#second graph: deaths per day
@app.callback(Output('second', 'figure'),
             [Input('country_drop', 'value')])
def summary_death(country):

    df = n_deaths[n_deaths['country'] == country].copy()    
    trace1 = [go.Bar(
                    x = df['date'],
                    y = df['cases'])]


    return {'data' : trace1, 'layout': go.Layout(title = 'Deaths per day: {c}'.format(c = country), xaxis = {'title' : 'Date'},
                              yaxis = {'title': '#'})}


@app.callback(Output('third', 'figure'),
             [Input('country_drop', 'value')])

def summary_combined(country):

    df = infection_type.groupby(['country', 'date', 'type']).sum()
    df.reset_index(inplace = True)
    df = qq[qq['country'] == country]
    df['confirmed'] = df[df['type'] == 'confirmed']['cases'].cumsum()
    df['deaths'] = df[df['type'] == 'death']['cases'].cumsum()
    df['recovered'] = df[df['type'] == 'recovered']['cases'].cumsum()
    df = df.fillna(0)
    df['total'] = df['confirmed'] + df['deaths'] + df['recovered']
    cdf = df[df['type'] == 'confirmed']
    ddf = df[df['type'] == 'death']
    rdf = df[df['type'] == 'recovered']

    fig = go.Figure()

    fig.add_trace(go.Scatter(x = cdf['date'],
                            y = cdf['total'],
                            line=dict(color='royalblue', width=2),
                            name = 'Confirmed'))

    fig.add_trace(go.Scatter(x = ddf['date'],
                            y = ddf['total'],
                            line=dict(color='firebrick', width=2),
                            name = 'Deaths'))

    fig.add_trace(go.Scatter(x = rdf['date'],
                            y = rdf['total'],
                            line=dict(color='green', width=2),
                            name = 'Recovered'))

    return fig


if __name__ == '__main__':
    app.run_server()

我使用了此处提供的基于 JHU 的冠状病毒数据集: https://github.com/RamiKrispin/coronavirus-csv

我的分析和数据操作参考在这里(这里没有 Dash 代码): https://www.kaggle.com/sandeshpatkar/coronavirus-worldwide-cases-analysis

您写 df = qq[qq['country'] == country] 时似乎有一个小错字,而您的意思可能是 df = df[df['country'] == country]。如果您将 qq 替换为 df,应用程序将按预期运行。