0

Hope someone has a suggestion! When I dont specify X and Y positions, the Sankey plot looks okay in terms of shape but not in terms of vertical ordering: the vertical/Y order of the elements is not what I want (for every X position the ordering in vertically is different):

enter image description here

When I set pad=100 the Y order is correct for every X position weird enough. But with pad=1, I tried to set X and Y positions explicitly but then the plot looks weird in terms of shape. Dont know why. Any ideas ? See the sample code below:

fig = go.Figure(go.Sankey(
arrangement = "snap",
node = {
    "label": ["0br","0cl","0go","0gr","0hy","1br","1cl","1go","1gr","1hy","2br","2cl","2go","2gr","2hy","3br","3cl","3go","3gr","3hy","4br","4cl","4go","4gr","4hy","5br","5cl","5go","5gr","5hy","6br","6cl","6go","6gr","6hy"],
    "x": [0, 0, 0, 0, 0, 0.16, 0.16, 0.16, 0.16, 0.16, 0.33, 0.33, 0.33, 0.33, 0.33, 0.5, 0.5, 0.5, 0.5, 0.5, 0.66, 0.66, 0.66, 0.66, 0.66, 0.84, 0.84, 0.84, 0.84, 0.84, 1, 1, 1, 1, 1],
    "y": [0, 0.25, 0.5, 0.75, 1.0,0, 0.25, 0.5, 0.75, 1.0,0, 0.25, 0.5, 0.75, 1.0,0, 0.25, 0.5, 0.75, 1.0,0, 0.25, 0.5, 0.75, 1.0,0, 0.25, 0.5, 0.75, 1.0,0, 0.25, 0.5, 0.75, 1.0],
    "color" : ['rgba(177, 86, 15, 0.85)','rgba(0, 0, 0, 1)','rgba(212,175,55, 0.8)','rgba(0, 153, 51, 0.8)','rgba(212,175,55, 0.8)',                   'rgba(177, 86, 15, 0.85)','rgba(0, 0, 0, 1)','rgba(212,175,55, 0.8)','rgba(0, 153, 51, 0.8)','rgba(212,175,55, 0.8)','rgba(177, 86, 15, 0.85)','rgba(0, 0, 0, 1)','rgba(212,175,55, 0.8)','rgba(0, 153, 51, 0.8)','rgba(212,175,55, 0.8)',                   'rgba(177, 86, 15, 0.85)','rgba(0, 0, 0, 1)','rgba(212,175,55, 0.8)','rgba(0, 153, 51, 0.8)','rgba(212,175,55, 0.8)',                   'rgba(177, 86, 15, 0.85)','rgba(0, 0, 0, 1)','rgba(212,175,55, 0.8)','rgba(0, 153, 51, 0.8)','rgba(212,175,55, 0.8)',                   'rgba(177, 86, 15, 0.85)','rgba(0, 0, 0, 1)','rgba(212,175,55, 0.8)','rgba(0, 153, 51, 0.8)','rgba(212,175,55, 0.8)',                   'rgba(177, 86, 15, 0.85)','rgba(0, 0, 0, 1)','rgba(212,175,55, 0.8)','rgba(0, 153, 51, 0.8)','rgba(212,175,55, 0.8)'],
    'pad':1,
    "thickness" : 20,
    "line" : dict(color = "black", width = 0.5),},
link = {
    "source": [0,0,0,0,0,1,1,1,1,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5,6,6,6,6,7,8,8,8,8,9,9,9,9,10,10,10,10,11,11,11,11,12,12,12,13,13,13,13,14,15,15,15,16,16,16,17,17,17,18,18,18,18,19,19,20,20,20,21,21,21,22,22,23,23,23,24,24,24,24,25,25,25,25,26,26,26,26,27,28,28,28,28,29],
    "target": [5,6,7,8,9,5,6,8,9,7,5,6,7,8,9,5,6,7,8,9,10,11,12,13,14,10,11,13,14,12,10,11,13,14,11,12,13,14,15,16,18,19,15,16,18,19,15,17,19,15,16,18,19,19,20,21,23,20,21,23,20,22,24,20,21,23,24,21,24,25,26,28,25,26,28,27,29,25,26,28,25,27,28,29,30,31,33,34,30,31,33,34,32,30,31,33,34,34],
    "value": [322,14,4,6,1,47,1177,36,26,37,8,17,1,209,6,1,4,97,5,333,228,103,5,33,9,110,1016,77,9,139,27,12,203,14,3,5,12,346,336,21,7,1,74,1014,40,6,2,146,1,22,2,286,15,378,377,38,19,4,978,55,2,143,1,1,3,322,7,1,400,362,6,16,9,960,51,139,4,9,9,378,5,1,1,401,377,1,4,3,2,845,127,1,140,11,14,420,1,405]
})) fig.show()

The correct vertical ordering should be br, cl, go, gr, hy for all the horizontal/X values 0...6. So it starts with 0br, 0cl, 0go, 0gr, 0hy and then to the right of it 1br, 1cl, 1go, 1gr, 1hy etc.

KR !

IvoB
  • 23
  • 7

1 Answers1

3
x = [0, 0, 0, 0, 0, 0.16, 0.16, 0.16, 0.16, 0.16, 0.33, 0.33, 0.33, 0.33, 0.33, 0.5, 0.5, 0.5, 0.5, 0.5, 0.66, 0.66, 0.66, 0.66, 0.66, 0.84, 0.84, 0.84, 0.84, 0.84, 1, 1, 1, 1, 1]
y = [0, 0.25, 0.5, 0.75, 1.0,0, 0.25, 0.5, 0.75, 1.0,0, 0.25, 0.5, 0.75, 1.0,0, 0.25, 0.5, 0.75, 1.0,0, 0.25, 0.5, 0.75, 1.0,0, 0.25, 0.5, 0.75, 1.0,0, 0.25, 0.5, 0.75, 1.0]
x = [.001 if v==0 else .999 if v==1 else v for v in x]
y = [.001 if v==0 else .999 if v==1 else v for v in y]
fig = go.Figure(go.Sankey(
arrangement = "snap",
node = {
    "label": ["0br","0cl","0go","0gr","0hy","1br","1cl","1go","1gr","1hy","2br","2cl","2go","2gr","2hy","3br","3cl","3go","3gr","3hy","4br","4cl","4go","4gr","4hy","5br","5cl","5go","5gr","5hy","6br","6cl","6go","6gr","6hy"],
    "x": x,
    "y": y,
    "color" : ['rgba(177, 86, 15, 0.85)','rgba(0, 0, 0, 1)','rgba(212,175,55, 0.8)','rgba(0, 153, 51, 0.8)','rgba(212,175,55, 0.8)',                   'rgba(177, 86, 15, 0.85)','rgba(0, 0, 0, 1)','rgba(212,175,55, 0.8)','rgba(0, 153, 51, 0.8)','rgba(212,175,55, 0.8)','rgba(177, 86, 15, 0.85)','rgba(0, 0, 0, 1)','rgba(212,175,55, 0.8)','rgba(0, 153, 51, 0.8)','rgba(212,175,55, 0.8)',                   'rgba(177, 86, 15, 0.85)','rgba(0, 0, 0, 1)','rgba(212,175,55, 0.8)','rgba(0, 153, 51, 0.8)','rgba(212,175,55, 0.8)',                   'rgba(177, 86, 15, 0.85)','rgba(0, 0, 0, 1)','rgba(212,175,55, 0.8)','rgba(0, 153, 51, 0.8)','rgba(212,175,55, 0.8)',                   'rgba(177, 86, 15, 0.85)','rgba(0, 0, 0, 1)','rgba(212,175,55, 0.8)','rgba(0, 153, 51, 0.8)','rgba(212,175,55, 0.8)',                   'rgba(177, 86, 15, 0.85)','rgba(0, 0, 0, 1)','rgba(212,175,55, 0.8)','rgba(0, 153, 51, 0.8)','rgba(212,175,55, 0.8)'],
    'pad':1,
    "thickness" : 20,
    "line" : dict(color = "black", width = 0.5),},
link = {
    "source": [0,0,0,0,0,1,1,1,1,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5,6,6,6,6,7,8,8,8,8,9,9,9,9,10,10,10,10,11,11,11,11,12,12,12,13,13,13,13,14,15,15,15,16,16,16,17,17,17,18,18,18,18,19,19,20,20,20,21,21,21,22,22,23,23,23,24,24,24,24,25,25,25,25,26,26,26,26,27,28,28,28,28,29],
    "target": [5,6,7,8,9,5,6,8,9,7,5,6,7,8,9,5,6,7,8,9,10,11,12,13,14,10,11,13,14,12,10,11,13,14,11,12,13,14,15,16,18,19,15,16,18,19,15,17,19,15,16,18,19,19,20,21,23,20,21,23,20,22,24,20,21,23,24,21,24,25,26,28,25,26,28,27,29,25,26,28,25,27,28,29,30,31,33,34,30,31,33,34,32,30,31,33,34,34],
    "value": [322,14,4,6,1,47,1177,36,26,37,8,17,1,209,6,1,4,97,5,333,228,103,5,33,9,110,1016,77,9,139,27,12,203,14,3,5,12,346,336,21,7,1,74,1014,40,6,2,146,1,22,2,286,15,378,377,38,19,4,978,55,2,143,1,1,3,322,7,1,400,362,6,16,9,960,51,139,4,9,9,378,5,1,1,401,377,1,4,3,2,845,127,1,140,11,14,420,1,405]
})) 

fig.show()

enter image description here

Rob Raymond
  • 29,118
  • 3
  • 14
  • 30
  • Amazing !! I missed that detail about no 0 and no 1 values....great and it works now !! Thanks for the helping hand ! – IvoB Feb 13 '22 at 14:57
  • Dear Rob Raymond, your reply helped me a lot but when the numbers get a bit more nasty, the specified X and Y positions are not maintained :( I dont have an idea how that is possible. See the example (without specifying X/Y the shape of the plot is okay, but the vertical ordering incorrect. With X/Y it gets messed up). – IvoB Feb 14 '22 at 22:33
  • if you search https://community.plotly.com you find there are multiple issues with positioning nodes. Solution I provided is sort of a work around (IMHO 0 and 1 should be valid) and I suspect there are other issues with plotly.js sankey node positioning which more obscure with plotly.py – Rob Raymond Feb 15 '22 at 12:19
  • okay thans @Rob! Do you suggest to use other ways to plot a decent Sankey diagram? – IvoB Feb 16 '22 at 13:17
  • I used data in your deleted answer - really should be a new question. anyway conclusion, you need links between all permutations of source and target, then it works – Rob Raymond Feb 16 '22 at 18:16
  • Hi Rob, I dont get your previous post. You mean that you have used my "nasty" data of a post you cannot see anymore....and you got it working by using al permutations, even with value = 0? – IvoB Feb 17 '22 at 21:32
  • yep I can see your hidden / deleted answer (my SO rep is sufficient ). if you take there are 6*6 links per col and 6 cols (don't count last one) there would be 216 values. you defined 157 values (and links). expand it all out to 216 and the sankey draws correctly. although value does have to be >0 – Rob Raymond Feb 17 '22 at 22:18
  • Okay Rob, thanks again. So what I did: provide all X and Y positions but no 1 and no 0 is used, provide all possible edges between the nodes, even the ones that are not used/equal to zero. Then the Sankey diagrams looks good to me. But I found out that also the values of the links cannot be equal to zero! So I had to adjust them also to 0.000001 but as a result any non zero link is drawn....so I have to much links drawn between the nodes now. Is there a way to instruct Plotly not to draw small valued links ? The "None" value is also not accepted just as a zero value screws up the diagram.... – IvoB Feb 19 '22 at 08:49
  • I tried a few different things but could not find a workaround to using link value to prevent it drawing. haven't really inspected javascript code of plotly library. I'd recommend raising an issue against https://community.plotly.com for me there appear to be some bugs in the library – Rob Raymond Feb 19 '22 at 09:43
  • It is a really helpful answer. The code did not warn it, but just not work when the x,y values <0 or >1. So I felt really weird before. – Xiaojian Chen Nov 23 '22 at 17:00