I have the following code that should plot a graph. 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:
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. 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
And my analysis and data manipulation for reference is here (No Dash code here): https://www.kaggle.com/sandeshpatkar/coronavirus-worldwide-cases-analysis
Advertisement
Answer
There seems to be a small typo as you wrote df = qq[qq['country'] == country]
while you probably meant df = df[df['country'] == country]
. If you replace qq
with df
the app works as expected.