I created a Sankey graph from a dataframe using this SO squestion. but my target shows undefined
when I hover the graph, my outflowing count also displays 0
below is my code and my dataframe.
# imports
import pandas as pd
import numpy as np
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
# Retrieve data
df = df[['Label1','value','year']]
dmap = {'cs.AI':0,'cs.CC':1,'cs.CE':2,'cs.CG':3,'cs.CL':4,'cs.CR':5}
df['source'] = df['Label1'].map(dmap)
df.sort_values(by=["source"], inplace=True)
nodes_color = ['#a6cee3','#fdbf6f','#fb9a99','#e3a6ce','#a6e3da','#a6e3bd']
L = [6,7,8,9,10,11,12,13,15,16,17,18,19,20,21,22,23,24,25]
df['target'] = df.groupby('source').cumcount().map(dict(enumerate(L)))
label = list(df['Label1'].unique())
target = list(df['target'])
value = list(df['value'])
source = list(df['source'])
link_color = np.repeat(np.array(['#a6cee3','#fdbf6f','#fb9a99','#e3a6ce','#a6e3da','#a6e3bd'], dtype=object), 18).tolist()
# Sankey plot setup
data_trace = dict(
type='sankey',
domain = dict(
x = [0,1],
y = [0,1]
),
orientation = "h",
valueformat = ".0f",
node = dict(
pad = 20,
# thickness = 30,
line = dict(
color = "black",
width = 0
),
label = label,
color =
),
link = dict(
source = source,
target = target,
value = value,
color = link_color,
)
)
layout = dict(
title = "Draw Sankey Diagram from dataframes",
height = 772,
font = dict(
size = 10),)
fig = dict(data=[data_trace], layout=layout)
iplot(fig, validate=False)
my dataframe
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
my Sankey graph.