You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何调整Graphviz子图布局:将红色边子图置于左下、绿色边子图置于右下

调整Graphviz子图布局实现左下角/右下角对齐

要实现红色子图在主图左下角、绿色子图在右下角的布局,需要利用Graphviz的集群属性和rank约束控制位置,具体修改如下:

修改后的完整代码

digraph "main" {
        graph [
                layout=dot,
                rankdir=LR,
                splines=ortho
        ];
        node [label="\N",
                shape=ellipse
        ];
        edge [color=black,
                weight=10
        ];
        
        0 [shape=rectangle]
        1 [shape=rectangle]
        0 -> 1 [color=blue, weight=10, label=S0]
        2 [shape=rectangle]
        1 -> 2 [color=blue, weight=10, label=S1]
        5 [shape=rectangle]
        2 -> 5 [color=blue, weight=100, label=S4]
        3 [shape=rectangle]
        2 -> 3 [label=S5]
        4 [shape=rectangle]
        2 -> 4 [label=S4]
        
        // 绿色子图(右下角),定义为集群
        subgraph cluster_green {
            graph [rankdir=RL, style=invis, labelloc=b, labeljust=r]
            edge[weight=1]
            11 [shape=rectangle]
            2 -> 11 [color=green, xlabel=M1]
            10 [shape=rectangle]
            11 -> 10 [color=green, xlabel=M2]
            10 -> 11 [color=green, xlabel=M3]
            11 -> 2 [color=green, xlabel=M4]
            // 强制集群与节点5同rank,对齐右下角
            {rank=same; 5; 10; 11}
        }
        
        // 红色子图(左下角),定义为集群
        subgraph cluster_red {
            graph [rankdir=LR, style=invis, labelloc=b, labeljust=l]
            edge[weight=8]
            12 [shape=rectangle]
            2 -> 12 [color=red, weight=10, xlabel=M5]
            12 -> 5 [color=red, xlabel=M6]
            5 -> 12 [color=red, xlabel=M7]
            12 -> 2 [color=red, xlabel=M8]
            // 强制集群与节点2同rank,对齐左下角
            {rank=same; 2; 12}
        }
}

关键修改点

  • 将子图转为集群:给子图命名添加cluster_前缀,让Graphviz识别为可控制布局的集群,同时设置style=invis隐藏集群边框(不需要边框时使用)
  • rank约束对齐:在每个集群内添加{rank=same; 主图节点; 子图节点},强制子图节点与主图目标节点处于同一水平rank:
    • 红色集群绑定主图节点2,实现左下角对齐
    • 绿色集群绑定主图节点5,实现右下角对齐
  • 集群布局微调:设置labelloc=b和labeljust(左/右),确保集群内部元素的对齐方向符合预期

修改后即可让两个子图分别固定在主图的左下角和右下角,达到目标布局效果。

内容的提问来源于stack exchange,提问作者Anandan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 07:20:53