graphviz 子图从上到下显示

graphviz subgraph show from top to down

我正在使用 graphviz 逐步绘制树的结构。

显示如下:

树应该从上到下显示,而不是从左到右显示。

上图构造代码:

digraph test1 {
    subgraph cluster000005 {
        node_1 [label="2, 4"]
        node_2 [label=1]
        node_1 -> node_2
        node_3 [label=3]
        node_1 -> node_3
        node_4 [label=5]
        node_1 -> node_4
        label="add 5"
    }
    subgraph cluster000004 {
        node_5 [label=2]
        node_6 [label=1]
        node_5 -> node_6
        node_7 [label="3, 4"]
        node_5 -> node_7
        label="add 4"
    }
    subgraph cluster000003 {
        node_8 [label=2]
        node_9 [label=1]
        node_8 -> node_9
        node_10 [label=3]
        node_8 -> node_10
        label="add 3"
    }
    subgraph cluster000002 {
        node_11 [label="1, 2"]
        label="add 2"
    }
    subgraph cluster000001 {
        node_12 [label=1]
        label="add 1"
    }
    rankdir=LR
}

这就是你想要的吗?

  • 删除 rankdir 声明
  • 将不可见边添加到堆叠簇(垂直)
  • 为了使集群很好地对齐而进行了大量调整 - 添加了周围的集群

这个:

digraph test1 {
    // added "invisible clusters" to get the "real clusters"
    //   to align vertically
    //   don't understand why this was necessary
    subgraph clusterInvis1 {
    subgraph cluster000005 {
        node_1 [label="2, 4" ]
        node_2 [label=1]
        node_3 [label=3]
        node_4 [label=5]
        node_1 -> node_2
        node_1 -> node_3
        node_1 -> node_4
        label="add 5"
    }
    subgraph clusterInvis2 {    
    subgraph cluster000004 {
        node_5 [label=2]
        node_6 [label=1]
        node_7 [label="3, 4"]
        node_5 -> node_6
        node_5 -> node_7
        label="add 4"
    }
    subgraph cluster000003 {
        node_8 [label=2 ]
        node_9 [label=1]
        node_10 [label=3 ]
        node_8 -> node_9
        node_8 -> node_10
        label="add 3"
    }
    peripheries=0  // place here for cluster only (not nodes)
    }
    peripheries=0  // place here for cluster only (not nodes)
    }
    subgraph cluster000002 {
        node_11 [label="1, 2" ]
        label="add 2"
    }
    subgraph cluster000001 {
        node_12 [label=1 ]
        label="add 1"
    }
    //rankdir=LR
    edge [style=invis]
    node_12 -> node_11 -> node_8
    // shorthand to define two edges
    {node_9 node_10} -> node_5  
    {node_6 node_7}  -> node_1
}

给出: