У меня есть следующий код, который должен построить график. Но он становится пустым.
@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')
])
])
Это первый раз, когда я пытаюсь построить линейный график, используя add_trace
в Да sh. Я построил другие графики, используя
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
И мой анализ и манипулирование данными для справки здесь (без Да sh код здесь): https://www.kaggle.com/sandeshpatkar/coronavirus-worldwide-cases-analysis