【发布时间】:2021-10-11 06:51:50
【问题描述】:
我一直在尝试使用plotly tutorial 在dash 应用程序上创建一个Sankey 图,但我没有成功。我一直遇到以下错误dash.exceptions.InvalidCallbackReturnValue: The callback ..my_bee_map.figure.. is a multi-output. 我可以弄清楚如何解决这个问题。
我的最终目标是查看标签和年份的流向,其值为>=0.75。这意味着当我选择标签时,例如cs.AI,我想查看labels、years 的流向,其中>=0.75 值与标签cs.AI 相关联
这是我的仪表板应用程序和绘图的值。
import pandas as pd
import plotly.express as px # (version 4.7.0 or higher)
import plotly.graph_objects as go
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import numpy as np
from dash import Dash # pip install dash (version 2.0.0 or higher)
app = Dash(__name__)
df = pd.read_csv("data.csv")
app.layout = html.Div([
html.H1("Web Application Dashboards with Dash", style={'text-align': 'center'}),
dcc.Dropdown(id="slct_label",
options=[{'label': x, 'value': x} for x in
sorted(df["Label1"].unique())],
multi=False,
value="cs.AI",
style={'width': "30%"}
),
html.Br(),
dcc.Dropdown(id="slct_value",
options=[{'label': x, 'value': x} for x in
sorted(df[df["value"] >= 0.75]["value"].unique())],
multi=False,
value=0.75,
style={'width': "40%"},
placeholder="Select threshold"
),
# html.Div(id='output_container', children=[]),
html.Br(),
dcc.Graph(id='my_bee_map', figure={})
])
# Callback - app interactivity section------------------------------------
@app.callback(
[Output(component_id='output_container', component_property='children'),
Output(component_id='my_bee_map', component_property='figure')],
[Input(component_id='slct_label', component_property='value'),
Input(component_id='slct_value', component_property='value')]
)
def update_graph(slct_label, slct_value):
# print(slct_label,slct_value)
# print(type(slct_label), type(slct_value))
container = "The year chosen by user was: {}".format(slct_label)
dff = df.copy()
# if slct_label:
dff = df[df['Label1'] != slct_label]
label = list(dff['Label1'].unique())
source = np.repeat(np.arange(0, 5), 18).tolist()
Value = list(dff['value'])
target = list(dff['Label1'])
color = np.repeat(np.array(['#a6cee3','#fdbf6f','#fb9a99','#e3a6ce','#a6e3da'],dtype=object), 18).tolist()
link = dict(source=source,target=target,value=Value,color=color)
node = dict(label=label,pad=35,thickness=15)
data = go.Sankey(link=link,node=node)
#graph the Sankey
fig = go.Figure(data)
fig.update_layout(
hovermode= 'x',
title = 'Migration from 1990 to 1992',
font=dict(size=10,color='white'),
paper_bgcolor='#51504f')
return fig, container
if __name__ == '__main__':
app.run_server()
我的数据
Label1 value year
0 cs.AI 1.00 1990
1 cs.AI 0.20 1990
2 cs.AI 0.85 1990
3 cs.AI 0.99 1990
4 cs.AI 0.19 1990
5 cs.AI 0.87 1990
6 cs.CC 0.19 1990
7 cs.CC 1.00 1990
8 cs.CC 0.34 1990
9 cs.CC 0.50 1990
10 cs.CC 0.09 1990
11 cs.CC 0.67 1990
12 cs.CE 0.94 1990
13 cs.CE 0.63 1990
14 cs.CE 1.00 1990
15 cs.CE 0.61 1990
16 cs.CE 0.82 1990
17 cs.CE 0.17 1990
18 cs.CG 0.74 1990
19 cs.CG 0.95 1990
20 cs.CG 0.53 1990
21 cs.CG 1.00 1990
22 cs.CG 0.43 1990
23 cs.CG 0.10 1990
24 cs.CL 0.31 1990
25 cs.CL 0.27 1990
26 cs.CL 0.91 1990
27 cs.CL 0.21 1990
28 cs.CL 1.00 1990
29 cs.CL 0.12 1990
30 cs.CR 0.31 1990
31 cs.CR 0.18 1990
32 cs.CR 0.76 1990
33 cs.CR 0.35 1990
34 cs.CR 0.67 1990
35 cs.CR 1.00 1990
36 cs.AI 1.00 1991
37 cs.AI 0.55 1991
38 cs.AI 0.82 1991
39 cs.AI 0.05 1991
40 cs.AI 0.17 1991
41 cs.AI 0.83 1991
42 cs.CC 0.52 1991
43 cs.CC 1.00 1991
44 cs.CC 0.64 1991
45 cs.CC 1.00 1991
46 cs.CC 0.80 1991
47 cs.CC 0.21 1991
48 cs.CE 0.10 1991
49 cs.CE 0.58 1991
50 cs.CE 1.00 1991
51 cs.CE 0.01 1991
52 cs.CE 0.77 1991
53 cs.CE 0.19 1991
54 cs.CG 0.08 1991
55 cs.CG 0.21 1991
56 cs.CG 0.63 1991
57 cs.CG 1.00 1991
58 cs.CG 0.34 1991
59 cs.CG 0.60 1991
60 cs.CL 0.91 1991
61 cs.CL 0.33 1991
62 cs.CL 0.60 1991
63 cs.CL 0.57 1991
64 cs.CL 1.00 1991
65 cs.CL 0.37 1991
66 cs.CR 1.00 1991
67 cs.CR 0.28 1991
68 cs.CR 0.92 1991
69 cs.CR 0.47 1991
70 cs.CR 0.53 1991
71 cs.CR 1.00 1991
72 cs.AI 1.00 1992
73 cs.AI 0.79 1992
74 cs.AI 0.86 1992
75 cs.AI 0.30 1992
76 cs.AI 0.27 1992
77 cs.AI 0.91 1992
78 cs.CC 0.06 1992
79 cs.CC 1.00 1992
80 cs.CC 0.72 1992
81 cs.CC 0.44 1992
82 cs.CC 0.31 1992
83 cs.CC 0.75 1992
84 cs.CE 0.40 1992
85 cs.CE 0.07 1992
86 cs.CE 1.00 1992
87 cs.CE 0.88 1992
88 cs.CE 0.79 1992
89 cs.CE 0.03 1992
90 cs.CG 0.74 1992
91 cs.CG 0.91 1992
92 cs.CG 1.00 1992
93 cs.CG 1.00 1992
94 cs.CG 0.68 1992
95 cs.CG 0.22 1992
96 cs.CL 0.42 1992
97 cs.CL 0.03 1992
98 cs.CL 0.95 1992
99 cs.CL 0.17 1992
100 cs.CL 1.00 1992
101 cs.CL 0.28 1992
102 cs.CR 0.04 1992
103 cs.CR 0.30 1992
104 cs.CR 0.26 1992
105 cs.CR 0.80 1992
106 cs.CR 0.90 1992
107 cs.CR 1.00 1992
【问题讨论】:
标签: python pandas plotly visualization plotly-dash