• <bdo id='kxQPf'></bdo><ul id='kxQPf'></ul>
      1. <small id='kxQPf'></small><noframes id='kxQPf'>

        <i id='kxQPf'><tr id='kxQPf'><dt id='kxQPf'><q id='kxQPf'><span id='kxQPf'><b id='kxQPf'><form id='kxQPf'><ins id='kxQPf'></ins><ul id='kxQPf'></ul><sub id='kxQPf'></sub></form><legend id='kxQPf'></legend><bdo id='kxQPf'><pre id='kxQPf'><center id='kxQPf'></center></pre></bdo></b><th id='kxQPf'></th></span></q></dt></tr></i><div id='kxQPf'><tfoot id='kxQPf'></tfoot><dl id='kxQPf'><fieldset id='kxQPf'></fieldset></dl></div>
        <tfoot id='kxQPf'></tfoot>

        <legend id='kxQPf'><style id='kxQPf'><dir id='kxQPf'><q id='kxQPf'></q></dir></style></legend>

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

        时间:2023-09-28

      2. <small id='bdaky'></small><noframes id='bdaky'>

            <tbody id='bdaky'></tbody>
          • <tfoot id='bdaky'></tfoot><legend id='bdaky'><style id='bdaky'><dir id='bdaky'><q id='bdaky'></q></dir></style></legend>
              <bdo id='bdaky'></bdo><ul id='bdaky'></ul>

                <i id='bdaky'><tr id='bdaky'><dt id='bdaky'><q id='bdaky'><span id='bdaky'><b id='bdaky'><form id='bdaky'><ins id='bdaky'></ins><ul id='bdaky'></ul><sub id='bdaky'></sub></form><legend id='bdaky'></legend><bdo id='bdaky'><pre id='bdaky'><center id='bdaky'></center></pre></bdo></b><th id='bdaky'></th></span></q></dt></tr></i><div id='bdaky'><tfoot id='bdaky'></tfoot><dl id='bdaky'><fieldset id='bdaky'></fieldset></dl></div>
                  本文介绍了Plotly:如何在桑基图中设置节点位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  样本数据如下:

                  unique_list = ['home0', 'page_a0', 'page_b0', 'page_a1', 'page_b1','page_c1'、'page_b2'、'page_a2'、'page_c2'、'page_c3']来源 = [0, 0, 1, 2, 2, 3, 3, 4, 4, 7, 6]目标 = [3, 4, 4, 3, 5, 6, 8, 7, 8, 9, 9]值 = [2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2]

                  使用

                  但是,我想获得在同一垂直列中以相同数字结尾的所有值,就像最左边一列的所有节点都以 0 结尾一样.我明白了在

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

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

                  完整代码:

                  导入 plotly.graph_objectsunique_list = ['home0', 'page_a0', 'page_b0', 'page_a1', 'page_b1','page_c1'、'page_b2'、'page_a2'、'page_c2'、'page_c3']来源 = [0, 0, 1, 2, 2, 3, 3, 4, 4, 7, 6]目标 = [3, 4, 4, 3, 5, 6, 8, 7, 8, 9, 9]值 = [2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2]def 节点化(节点名称):节点名称 = 唯一列表# 独特的名字结尾结束 = sorted(list(set([e[-1] for e in node_names])))# 间隔步数 = 1/len(结束)# 每个唯一名称结尾的 x 值# 作为节点位置的输入节点_x = {}xVal = 0对于 e in 结束:节点x[str(e)] = xValxVal += 步数# 列表形式的 x 和 y 值x_values = [nodes_x[n[-1]] for n in node_names]y_values = [0.1]*len(x_values)返回 x_values, y_valuesnodified = nodify(node_names=unique_list)# 情节设置fig = go.Figure(data=[go.Sankey(安排='捕捉',节点 = 字典(垫= 15,厚度 = 20,line = dict(color = "black", width = 0.5),标签 = 唯一列表,颜色=蓝色",x=nodified[0],y=nodified[1]),链接=字典(来源=来源,目标=目标,价值 = 价值观))])图.show()

                  The sample data is as follows:

                  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]
                  

                  Using the sample code from the documentation

                  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()
                  

                  This outputs the following sankey diagram

                  However, I would like to get all the values which end in the same number in the same vertical column, just like how the leftmost column has all of it's nodes ending with a 0. I see in the docs that it is possible to move the node positions, however I was wondering if there was a cleaner way to do it other than manually inputting x and y values. Any help appreciated.

                  解决方案

                  In go.Sankey() set arrangement='snap' and adjust x and y positions in x=<list> and y=<list>. The following setup will place your nodes as requested.

                  Plot:

                  Please note that the y-values are not explicitly set in this example. As soon as there are more than one node for a common x-value, the y-values will be adjusted automatically for all nodes to be displayed in the same vertical position. If you do want to set all positions explicitly, just set arrangement='fixed'

                  Edit:

                  I've added a custom functin nodify() that assigns identical x-positions to label names that have a common ending such as '0' in ['home0', 'page_a0', 'page_b0']. Now, if you as an example change page_c1 to page_c2 you'll get this:

                  Complete code:

                  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()
                  

                  这篇关于Plotly:如何在桑基图中设置节点位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Plotly:具有多个轴的分组条形图 下一篇:Plotly:将线添加到条形图

                  相关文章

                • <small id='NLqot'></small><noframes id='NLqot'>

                      <bdo id='NLqot'></bdo><ul id='NLqot'></ul>

                  1. <i id='NLqot'><tr id='NLqot'><dt id='NLqot'><q id='NLqot'><span id='NLqot'><b id='NLqot'><form id='NLqot'><ins id='NLqot'></ins><ul id='NLqot'></ul><sub id='NLqot'></sub></form><legend id='NLqot'></legend><bdo id='NLqot'><pre id='NLqot'><center id='NLqot'></center></pre></bdo></b><th id='NLqot'></th></span></q></dt></tr></i><div id='NLqot'><tfoot id='NLqot'></tfoot><dl id='NLqot'><fieldset id='NLqot'></fieldset></dl></div>

                    <tfoot id='NLqot'></tfoot>
                    <legend id='NLqot'><style id='NLqot'><dir id='NLqot'><q id='NLqot'></q></dir></style></legend>