Plotly Sankey 图:如何设置节点位置,显式 X 和 Y 值不起作用

Plotly Sankey Diagram: how to set position of nodes, explicit X and Y values dont work

望大侠指教! 当我不指定 X 和 Y 位置时,Sankey 图在形状方面看起来不错,但在垂直排序方面却不行:元素的 vertical/Y 顺序不是我想要的(对于每个 X 位置,垂直排序不同):

当我设置 pad=100 时,Y 顺序对于每个 X 位置都是正确的,这很奇怪。但是在 pad=1 的情况下,我试图明确设置 X 和 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": [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()

对于所有 horizontal/X 值 0...6,正确的垂直顺序应该是 br、cl、go、gr、hy。所以它以 0br、0cl、0go、0gr、0hy 开头,然后在它的右边是 1br、1cl、1go、1gr、1hy 等

韩国!

  • 另见
  • xy 需要 >0 且 <1。如果 0 或 1 格式不起作用
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()