亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Plotly:如何在?;鶊D中設置節點位置?

Plotly:如何在?;鶊D中設置節點位置?

搖曳的薔薇 2022-10-06 15:57:17
樣本數據如下: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]使用文檔中的示例代碼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()這將輸出以下 sankey 圖但是,我想在同一垂直列中獲取以相同數字結尾的所有值,就像最左邊一列的所有節點都以 0 結尾一樣。我在文檔中看到可以移動節點位置,但是我想知道是否有比手動輸入 x 和 y 值更簡潔的方法。任何幫助表示贊賞。
查看完整描述

1 回答

?
HUH函數

TA貢獻1836條經驗 獲得超4個贊

在和 中go.Sankey()設置和調整 x和arrangement='snap'y 位置。以下設置將按要求放置您的節點。x=<list>y=<list>

陰謀:

http://img1.sycdn.imooc.com//633e8a850001889804790288.jpg

請注意,此示例中未明確設置 y 值。一旦一個公共 x 值有多個節點,y 值將自動調整以使所有節點顯示在相同的垂直位置。如果您確實想明確設置所有位置,只需設置arrangement='fixed'

編輯:

我添加了一個自定義函數nodify(),該函數將相同的 x 位置分配給具有共同結尾的標簽名稱,例如'0'in ['home0', 'page_a0', 'page_b0']?,F在,如果你作為一個例子改變page_c1page_c2會得到這個:

http://img1.sycdn.imooc.com//633e8a8e0001960805210305.jpg

完整代碼:


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



查看完整回答
反對 回復 2022-10-06
  • 1 回答
  • 0 關注
  • 614 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號