[英]Returning figure in Plotly Dash
我有以下代码应该 plot 一个图表。 但是,它是空白的。
@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
来 plot 折线图。 我已经绘制了其他图表使用
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
,则应用程序将按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.