python 在Plotly树图中可以改变矩形的顺序吗?

5vf7fwbs  于 2022-12-21  发布在  Python
关注(0)|答案(1)|浏览(149)

我想按照预定义的顺序创建一个 * treemap *,但是我不知道如何操作由plotly * treemap * 定义的层次结构。
下面是我创建的 * 树形图 *:

在这里,我希望O位于U框的上方,UR是唯一的列,而不使用NA分割其空间。
下面是我的代码:

import plotly.graph_objects as go

fig = go.Figure(go.Treemap(
    labels = ["DR","1,2","D 1","D 2","O","U",
              "3,4,5","P","B","Pe",
              "6,7,8","F","C","Pl",
              "9","UR",
              "10","NA"],
    parents = ["", "DR", "1,2","1,2","1,2","1,2","DR","3,4,5","3,4,5","3,4,5",
               "DR","6,7,8","6,7,8","6,7,8",
               "DR","9",
                "DR","10"],
    marker_colors = ["lightgrey", 
                      "lightblue", "cornflowerblue", "cornflowerblue","goldenrod","green",
                      "lightblue","lightgray","chocolate","cadetblue",
                      "lightblue","grey",'chocolate',"saddlebrown",
                      "salmon","salmon",
                      "burlywood","burlywood",
                      ],
    branchvalues="total"
))

fig.update_layout(margin = dict(t=10, l=60, r=25, b=30))
fig.show()

非常感谢!
我试过为每个矩形创建值,但没有帮助,也没有像预期的那样工作。

klr1opcd

klr1opcd1#

你需要参数tiling,我想这就是你要的。

import plotly.graph_objects as go

fig = go.Figure(go.Treemap(
    labels = ["DR","1,2","D 1","D 2","O","U",
              "3,4,5","P","B","Pe",
              "6,7,8","F","C","Pl",
              "9","UR",
              "10","NA"],
    parents = ["", "DR", "1,2","1,2","1,2","1,2","DR","3,4,5","3,4,5","3,4,5",
               "DR","6,7,8","6,7,8","6,7,8",
               "DR","9",
                "DR","10"],
    marker_colors = ["lightgrey", 
                      "lightblue", "cornflowerblue", "cornflowerblue","goldenrod","green",
                      "lightblue","lightgray","chocolate","cadetblue",
                      "lightblue","grey",'chocolate',"saddlebrown",
                      "salmon","salmon",
                      "burlywood","burlywood",
                      ],
    branchvalues="total",
    tiling = dict(packing='slice-dice')
))

相关问题