Plotly:如何在桑基图中设置节点位置?

bbd*_*108 10 python plotly plotly-python

样本数据如下:

unique_list = ['home0', 'page_a0', 'page_b0', 'page_a1', 'page_b1', 
               'page_c1', 'page_b2', 'page_a2', 'page_c2', 'page_c3']
sources = [0, 0, 1, 2, 2, 3, 3, 4, 4, 7, 6]
targets = [3, 4, 4, 3, 5, 6, 8, 7, 8, 9, 9]
values = [2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2]
Run Code Online (Sandbox Code Playgroud)

使用文档中的示例代码

fig = go.Figure(data=[go.Sankey(
    node = dict(
      pad = 15,
      thickness = 20,
      line = dict(color = "black", width = 0.5),
      label = unique_list,
      color = "blue"
    ),
    link = dict(
      source = sources,
      target = targets,
      value = values
  ))])

fig.show()
Run Code Online (Sandbox Code Playgroud)

这将输出以下桑基图

在此输入图像描述

但是,我想获取同一垂直列中以相同数字结尾的所有值,就像最左边的列的所有节点都以 0 结尾一样。我在文档中看到可以移动节点位置,但是我想知道除了手动输入 x 和 y 值之外是否有更干净的方法来做到这一点。任何帮助表示赞赏。

ves*_*and 15

在和中go.Sankey()设置arrangement='snap'并调整 x 和 y 位置。以下设置将根据要求放置您的节点。x=<list>y=<list>

阴谋:

在此输入图像描述

请注意,本示例中未明确设置 y 值。一旦有多个节点具有相同的 x 值,y 值将自动调整,以使所有节点显示在同一垂直位置。如果您确实想明确设置所有位置,只需设置arrangement='fixed'

编辑:

我添加了一个自定义函数nodify(),该函数将相同的 x 位置分配给具有共同结尾(例如'0'中)的标签名称['home0', 'page_a0', 'page_b0']page_c1现在,如果您更改为示例,page_c2您将得到以下结果:

在此输入图像描述

完整代码:

import plotly.graph_objects as go
unique_list = ['home0', 'page_a0', 'page_b0', 'page_a1', 'page_b1', 
               'page_c1', 'page_b2', 'page_a2', 'page_c2', 'page_c3']
sources = [0, 0, 1, 2, 2, 3, 3, 4, 4, 7, 6]
targets = [3, 4, 4, 3, 5, 6, 8, 7, 8, 9, 9]
values = [2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2]


def nodify(node_names):
    node_names = unique_list
    # uniqe name endings
    ends = sorted(list(set([e[-1] for e in node_names])))
    
    # intervals
    steps = 1/len(ends)

    # x-values for each unique name ending
    # for input as node position
    nodes_x = {}
    xVal = 0
    for e in ends:
        nodes_x[str(e)] = xVal
        xVal += steps

    # x and y values in list form
    x_values = [nodes_x[n[-1]] for n in node_names]
    y_values = [0.1]*len(x_values)
    
    return x_values, y_values

nodified = nodify(node_names=unique_list)

# plotly setup
fig = go.Figure(data=[go.Sankey(
      arrangement='snap',
      node = dict(
      pad = 15,
      thickness = 20,
      line = dict(color = "black", width = 0.5),
      label = unique_list,
      color = "blue",
     x=nodified[0],
     y=nodified[1]
    ),
    link = dict(
      source = sources,
      target = targets,
      value = values
  ))])

fig.show()
Run Code Online (Sandbox Code Playgroud)

  • @Lan 似乎会忽略“x”参数,除非您还包含“y”。类似地,如果“y”只是一个零数组。不过,最终布局*确实*似乎受到“y”值的影响。@vestland 使用常数 0.1 似乎也为我提供了最好的结果。 (2认同)