简体   繁体   English

Plotly Dash 中的返回图

[英]Returning figure in Plotly Dash

I have the following code that should plot a graph.我有以下代码应该 plot 一个图表。 But, it's coming up blank.但是,它是空白的。

@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

The html component code for the same is:相同的 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')
                        ])
])

This is the first time I am trying to plot a line graph using add_trace in Dash.这是我第一次尝试在 Dash 中使用add_trace来 plot 折线图。 I have plotted other graphs using我已经绘制了其他图表使用

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

or something similar.或类似的东西。 But that is not working for this code.但这不适用于此代码。 Please guide me.请指导我。 Thank you!谢谢!

Here's the full code for the app:这是该应用程序的完整代码:

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()

I have used the JHU based Coronavirus dataset available here: https://github.com/RamiKrispin/coronavirus-csv我使用了此处可用的基于 JHU 的冠状病毒数据集: https://github.com/RamiKrispin/coronavirus-csv

And my analysis and data manipulation for reference is here (No Dash code here): https://www.kaggle.com/sandeshpatkar/coronavirus-worldwide-cases-analysis我的分析和数据操作供参考(此处没有 Dash 代码): https://www.kaggle.com/sandeshpatkar/coronavirus-worldwide-cases-analysis

There seems to be a small typo as you wrote df = qq[qq['country'] == country] while you probably meant df = df[df['country'] == country] .你写的df = qq[qq['country'] == country]似乎有一个小错字,而你的意思可能是df = df[df['country'] == country] If you replace qq with df the app works as expected.如果您将qq替换为df ,则应用程序将按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM