0

I am working on Sankey Diagram using Plotly. The link line is not correctly aligned between a node. I guess the plot automatically put the highest source value to the top. Could you please advise how to fix this issue?

enter image description here

df_grouped_perc = df_grouped_perc.reindex(columns=['A', 'B', 'C', 'D', 'E','F'])

# Create a list of unique labels from the data
labels = df_grouped_perc[['A', 'B', 'C', 'D', 'E']].stack().unique().tolist()

# Create a dictionary to map labels to indices
label_dict = {label: i for i, label in enumerate(labels)}

# Initialize empty lists for source, target and value
source = []
target = []
value = []

# Loop over each row of the dataframe
for index, row in df_grouped_perc.iterrows():
    source.append(label_dict[row['A']])
    target.append(label_dict[row['B']])
    value.append(row['F'])
    
    source.append(label_dict[row['B']])
    target.append(label_dict[row['C']])
    value.append(row['F'])

# Create the link dictionary with the updated lists
link = dict(
    source=source,
    target=target,
    value=value,
    line=dict(width=value),
)

# Add the width list to the node dictionary
node = dict(
    pad=5,
    thickness=30,
    color='blue',
)

# Create the Sankey diagram trace
fig = go.Figure(data=[go.Sankey(
    node=node, # use the updated node dictionary
    link=link, # use the updated link dictionary
)])

fig.show()
  • I found out a solution. I realize the Plotly Sankey Diagram allows users to move the Node manually, by setting the node arrangement 'Snap', 'Freeform' etc. Thus, if someone has similar issue like me, you have to do puzzle between many columns. – stevekkong Apr 20 '23 at 17:24

0 Answers0