Plotly Sankey: How to sort target nodes
Question:
I’m currently trying my hands on the Sankey class of the plotly library of Python.
My problem is that when I specify targets of my links, the will always appear ordered by their values, instead of in the order of the values, for example:
labels = ["Source", "T1", "T2", "T3"]
source = [0, 0, 0]
target = [1, 2, 3]
value = [1, 3, 2]
node = dict(label=label)
link = dict(source=source, target=target, value=value)
fig = plotly.graph_objects.Figure(data=[plotly.graph_objects.Sankey(
node = node,
link = link
)])
fig.show()
I would like the targets to be displayed in the order T1, T2, T3, but I’m getting T2, T3, T1, because they are ordered by their value.
Is there any elegant way to get them ordered in the right way?
Answers:
You will need to set the x and y coordinates for the nodes to let it show up in the correct order. Look at the node position example here. (0,0) is top left of figure. One thing that is NOT there, but I learnt the hard way is that plotly does not like 0 and 1 for its coordinates (don’t know why exactly). So, set the coordinates as 0.0001 and 0.999 instead or 0 and 1. Also, you will need to set arrangement to snap. See the updated code below.
import plotly.graph_objects as go
labels = ["Source", "T1", "T2", "T3"]
source = [0, 0, 0]
target = [1, 2, 3]
value = [1, 3, 2]
x=[0.0001,0.9999,0.9999,0.9999] ## Source is x=0 and others are x=1
y=[0.0001,0.0001,1/6,4/6] ##Source is y=0, T1 starts at 0, T2 starts at fraction of total (6)...
node = dict(label=labels, x=x, y=y)
link = dict(source=source, target=target, value=value)
fig = go.Figure(data=[go.Sankey(arrangement = "snap", ##Note - added snap
node = node, link = link
)])
fig.show()
Plot
I’m currently trying my hands on the Sankey class of the plotly library of Python.
My problem is that when I specify targets of my links, the will always appear ordered by their values, instead of in the order of the values, for example:
labels = ["Source", "T1", "T2", "T3"]
source = [0, 0, 0]
target = [1, 2, 3]
value = [1, 3, 2]
node = dict(label=label)
link = dict(source=source, target=target, value=value)
fig = plotly.graph_objects.Figure(data=[plotly.graph_objects.Sankey(
node = node,
link = link
)])
fig.show()
I would like the targets to be displayed in the order T1, T2, T3, but I’m getting T2, T3, T1, because they are ordered by their value.
Is there any elegant way to get them ordered in the right way?
You will need to set the x and y coordinates for the nodes to let it show up in the correct order. Look at the node position example here. (0,0) is top left of figure. One thing that is NOT there, but I learnt the hard way is that plotly does not like 0 and 1 for its coordinates (don’t know why exactly). So, set the coordinates as 0.0001 and 0.999 instead or 0 and 1. Also, you will need to set arrangement to snap. See the updated code below.
import plotly.graph_objects as go
labels = ["Source", "T1", "T2", "T3"]
source = [0, 0, 0]
target = [1, 2, 3]
value = [1, 3, 2]
x=[0.0001,0.9999,0.9999,0.9999] ## Source is x=0 and others are x=1
y=[0.0001,0.0001,1/6,4/6] ##Source is y=0, T1 starts at 0, T2 starts at fraction of total (6)...
node = dict(label=labels, x=x, y=y)
link = dict(source=source, target=target, value=value)
fig = go.Figure(data=[go.Sankey(arrangement = "snap", ##Note - added snap
node = node, link = link
)])
fig.show()
Plot