【发布时间】:2021-03-14 13:52:13
【问题描述】:
我正在尝试为我拥有的时间序列数据框创建一个基本的交互式绘图。我已将数据读入数据框df。它有datetime 作为索引和另外两个列category(有3 个唯一值),count。
现在,我正在尝试使用具有以下功能的 plotly-dash 绘制交互式图表
- 它将有一个输入框,用户应在其中输入他们想要查看其绘图的
category值 - 如果他们输入的值在
df['category'].unique()中,它将返回对应于该特定类别的时间序列图。否则会报错
这是我为它编写的代码
import dash
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import plotly.express as px
app = dash.Dash()
app.layout = html.Div(children = [
html.H1(children='Dash trail'),
html.Br(),
html.Br(),
dcc.Input(id='input_id',value='',type='text'),
dcc.Graph(id='inflow_graph')
])
@app.callback(
[Output(component_id='inflow_graph',component_property='figure')],
[Input(component_id='input_id',component_property='value')])
def update_graph(input_text):
if input_text in df['category'].unique():
dff = df[df['category']==input_text]
fig = px.line(dff, x=dff.index, y=dff['count'],title='count for selected category')
return fig
else:
return 'Enter the correct category value'
if __name__=='__main__':
app.run_server(debug=True,use_reloader=False)
抛出以下错误
dash.exceptions.InvalidCallbackReturnValue: The callback ..inflow_graph.figure.. is a multi-output.
Expected the output type to be a list or tuple but got:
Figure({
'data': [{'hovertemplate': 'ds=%{x}<br>ticket_count=%{y}<extra></extra>',
'legendgroup': '',
'line': {'color': '#636efa', 'dash': 'solid'},
'mode': 'lines',
'name': '',
'showlegend': False,
'type': 'scattergl',
'x': array([datetime.datetime(2020, 1, 3, 0, 0), ....(and so on my full dataframe)
我不明白我在哪里回调多个输出。如何解决此错误?
编辑:在此处添加示例数据
Category Count
date(index)
2020-01-03 A 30
2020-01-03 B 50
2020-01-04 C 14
2020-01-04 A 16
2020-01-04 B 40
【问题讨论】:
-
请分享您的数据样本,以使您的代码可重现。
-
在编辑中添加了示例数据
标签: python pandas callback plotly plotly-dash