在Graphviz中的子图下添加标签

在Graphviz中的子图下添加标签,graphviz,Graphviz,我使用以下代码在Graphviz中使用点生成图形。我已经手动包含了节点的坐标,因为我需要四个彼此相邻的不相交子图,如图所示。 我希望在每个子图下添加标签:$G_0$,$G_1$,等等。在子图下添加标签会创建一个框,并忽略我的坐标对齐。还有其他方法吗,比如在指定坐标处放置任意文本?我使用“dot-Teps-Kfdp-n trees-o t.eps”进行编译 digraph Trees { node [shape=circle, style="filled", fixedsize=true,wid

我使用以下代码在Graphviz中使用点生成图形。我已经手动包含了节点的坐标,因为我需要四个彼此相邻的不相交子图,如图所示。

我希望在每个子图下添加标签:$G_0$,$G_1$,等等。在子图下添加标签会创建一个框,并忽略我的坐标对齐。还有其他方法吗,比如在指定坐标处放置任意文本?我使用“dot-Teps-Kfdp-n trees-o t.eps”进行编译

digraph Trees {
node [shape=circle, style="filled", fixedsize=true,width=0.6];  0; 1;2; 3;4; 5;6; 7; 8;9;10;11;12;13;14;15;
0[pos = "0,1!"]
1[fillcolor=red, pos = "-1,2!"]
2[pos = "1,2!"]
3 [pos = "0,-0.5!"]
5[label=1, fillcolor=red, pos = "2,2!"]
4[label=0, fillcolor=red, pos = "3,1!"]
6[label=2, pos = "4,2!"]
7[label=3, pos = "3, -0.5!"]
9[label=1, fillcolor=red, pos = "5,2!"]
8[label=0, fillcolor=red, pos = "6,1!"]
10[label=2, pos = "7,2!"]
11[label=3, fillcolor=red, pos = "6, -0.5!"]
12[label=1, fillcolor=red, pos = "8,2!"]
13[label=0, fillcolor=green, pos = "9,1!"]
14[label=2, pos = "10, 2!"]
15[label=3, fillcolor=green, pos = "9, -0.5!"]

overlap=false;
fontsize=10;

subgraph 1{
  edge [dir=none] 1->0 2->0 3->0;
}

subgraph 2{
  edge [color=red] 5->4;
  edge[color=black, dir=none] 6->4 7->4;
}

subgraph 3{
  edge [color=red] 9->8 8->11;
  edge [color=black, dir=none] 8->10;
}

subgraph 4{
  edge [color=green] 12->13; 13->15;
  edge [color=black, dir=none] 13->14;
}
}

不使用显式节点位置,您可以使用一个简单的有向图,结合一些秩约束不可见边文本节点,而不是子图标签

digraph Trees {
fontsize=10;
node [shape=circle, style="filled", fixedsize=true,width=0.6];
{rank=same;
  a1[label=1, fillcolor=red];
  a2[label=2];
  a3[label=1, fillcolor=red];
  a4[label=2];
  a5[label=1, fillcolor=red];
  a6[label=2];
  a7[label=1, fillcolor=red];
  a8[label=2];
}
node[label=0];
b1;
b2[fillcolor=red];
b3[fillcolor=red];
b4[fillcolor=green];

node[label=3];
c1;
c2;
c3[fillcolor=red];
c4[fillcolor=green];
node[shape=none, fillcolor=transparent];
d1[label="Label 1"];
d2[label="Label 2"];
d3[label="Label 3"];
d4[label="Label 4"];

edge[dir=none];
a1->b1;
a2->b1;
b1->c1;
c1->d1[style=invis];

a3->b2[dir=forward, fillcolor=red, color=red];
a4->b2;
b2->c2;
c2->d2[style=invis];

a5->b3[dir=forward, fillcolor=red, color=red];
a6->b3[dir=forward, fillcolor=red, color=red];
b3->c3;
c3->d3[style=invis];

a7->b4[dir=forward, fillcolor=green, color=green];
a8->b4[dir=forward, fillcolor=green, color=green];
b4->c4;
c4->d4[style=invis];

edge[style=invis];
a2 -> a3;
a4 -> a5;
a6 -> a7;
}

可以使用群集和点布局引擎使子图不相交。同样的方法也允许引入集群标签。可以根据需要将它们放置在集群的底部,而无需创建虚拟节点

这样,就不需要绝对位置,即使添加了其他节点,也会自动生成布局。节点的确切位置会发生变化,但图形在拓扑上保持不变

digraph Trees { node [shape = circle, style = "filled", fixedsize = true, width=0.4]; 
    edge [dir = none];
    layout = dot; overlap = false; fontsize = 10;
    graph [labelloc = "b", penwidth = 0];
    {   node [fillcolor = "red"];
        1; 5 [label = 1]; 4 [label = 0]; 9 [label = 1]; 
        8 [label = 0]; 11 [label = 3]; 12 [label = 1]; 
    }

    2; 0; 3; 6 [label = 2]; 7 [label = 3];
    10 [label = 2]; 14 [label = 2]; 
    {   node [fillcolor = "green"];
        13 [label = 0]; 15 [label = 3];
    }
    subgraph cluster1{ 
        label = "Subgraph 1"; 
        { 1; 2; } -> 0 -> 3; 
    }
    subgraph cluster2{ 
        label = "Subgraph 2"; 
        5 -> 4 [color = red, dir = fwd]; 
        6 -> 4 -> 7; 
    }
    subgraph cluster3{ 
        label = "Subgraph 3"; 
        9 -> 8-> 11 [color=red, dir = fwd]; 
        10 -> 8 [color=black]; 
    }
    subgraph cluster4{ 
        label = "Subgraph 4"; 
        12 -> 13 -> 15 [color=green, dir = fwd]; 
        14-> 13; 
    } 
}