如何在graphviz中固定节点的相对顺序?

如何在graphviz中固定节点的相对顺序?,graphviz,Graphviz,我想用graphviz画一个神经网络。以下是我的照片当前的样子: 如您所见,节点标签的顺序显示不正确。例如,我希望$a_0^(2)$粘贴到第2层的顶部,并且我希望以数字递增顺序排列同一层中的其余节点(即$a_1^(2)$,$a_2^(2)$,…) 我该怎么做?非常感谢 这是我的密码: digraph G { rankdir=LR; splines=line; nodesep=".05"; edge [comment="Wildcard node added au

我想用graphviz画一个神经网络。以下是我的照片当前的样子:

如您所见,节点标签的顺序显示不正确。例如,我希望$a_0^(2)$粘贴到第2层的顶部,并且我希望以数字递增顺序排列同一层中的其余节点(即$a_1^(2)$,$a_2^(2)$,…)

我该怎么做?非常感谢

这是我的密码:

digraph G {
    rankdir=LR;
    splines=line;
    nodesep=".05";
    edge [comment="Wildcard node added automatic in EG."];
    node [label=""];
    subgraph cluster_0 {
        color=white;
        label="layer 1 (input layer)";
        edge [comment="Wildcard node added automatic in EG."];
        node [color=chartreuse, 
              style=filled, 
              shape=circle];
        x0 [color=yellow, 
            fillcolor=yellow, 
            label=<x<sub>0</sub>>];
        x1 [fillcolor=chartreuse, 
            label=<x<sub>1</sub>>];
        x2 [fillcolor=chartreuse, 
            label=<x<sub>2</sub>>];
        x3 [fillcolor=chartreuse, 
            label=<x<sub>3</sub>>];
    }

    subgraph cluster_1 {
        color=white;
        label="layer 2 (hidden layer)";
        edge [comment="Wildcard node added automatic in EG."];
        node [color=dodgerblue, 
              style=filled, 
              shape=circle];
        a02 [color=yellow, 
             label=<a<sub>0</sub><sup>(2)</sup>>, 
             pos="0,0", 
             fillcolor=yellow];
        a12 [label=<a<sub>1</sub><sup>(2)</sup>>, 
             pos="0,-1", 
             fillcolor=dodgerblue];
        a22 [label=<a<sub>2</sub><sup>(2)</sup>>, 
             pos="0,-2", 
             fillcolor=dodgerblue];
        a32 [label=<a<sub>3</sub><sup>(2)</sup>>, 
             pos="0,-3", 
             fillcolor=dodgerblue];
        a42 [label=<a<sub>4</sub><sup>(2)</sup>>, 
             pos="0,-4", 
             fillcolor=dodgerblue];
        a52 [label=<a<sub>5</sub><sup>(2)</sup>>, 
             pos="0,-5", 
             fillcolor=dodgerblue];
    }

    subgraph cluster_2 {
        color=white;
        label="layer 3 (hidden layer)";
        edge [comment="Wildcard node added automatic in EG."];
        node [color=dodgerblue, 
              style=filled, 
              shape=circle];
        a03 [color=yellow, 
             fillcolor=yellow, 
             label=<a<sub>0</sub><sup>(3)</sup>>];
        a13 [fillcolor=dodgerblue, 
             label=<a<sub>1</sub><sup>(3)</sup>>];
        a23 [fillcolor=dodgerblue, 
             label=<a<sub>2</sub><sup>(3)</sup>>];
        a33 [fillcolor=dodgerblue, 
             label=<a<sub>3</sub><sup>(3)</sup>>];
        a43 [fillcolor=dodgerblue, 
             label=<a<sub>4</sub><sup>(3)</sup>>];
        a53 [fillcolor=dodgerblue, 
             label=<a<sub>5</sub><sup>(3)</sup>>];
    }

    subgraph cluster_3 {
        color=white;
        label="layer 4 (output layer)";
        edge [comment="Wildcard node added automatic in EG."];
        node [color=coral1, 
              style=filled, 
              shape=circle];
        O1 [fillcolor=coral1, 
            label=<a<sub>1</sub><sup>(4)</sup>>];
        O2 [fillcolor=coral1, 
            label=<a<sub>2</sub><sup>(4)</sup>>];
        O3 [fillcolor=coral1, 
            label=<a<sub>3</sub><sup>(4)</sup>>];
        O4 [fillcolor=coral1, 
            label=<a<sub>4</sub><sup>(4)</sup>>];
    }

    x0 -> a12;
    x0 -> a22;
    x0 -> a32;
    x0 -> a42;
    x0 -> a52;
    x1 -> a12;
    x1 -> a22;
    x1 -> a32;
    x1 -> a42;
    x1 -> a52;
    x2 -> a12;
    x2 -> a22;
    x2 -> a32;
    x2 -> a42;
    x2 -> a52;
    x3 -> a12;
    x3 -> a22;
    x3 -> a32;
    x3 -> a42;
    x3 -> a52;
    a02 -> a13;
    a02 -> a23;
    a02 -> a33;
    a02 -> a43;
    a02 -> a53;
    a12 -> a13;
    a12 -> a23;
    a12 -> a33;
    a12 -> a43;
    a12 -> a53;
    a22 -> a13;
    a22 -> a23;
    a22 -> a33;
    a22 -> a43;
    a22 -> a53;
    a32 -> a13;
    a32 -> a23;
    a32 -> a33;
    a32 -> a43;
    a32 -> a53;
    a42 -> a13;
    a42 -> a23;
    a42 -> a33;
    a42 -> a43;
    a42 -> a53;
    a52 -> a13;
    a52 -> a23;
    a52 -> a33;
    a52 -> a43;
    a52 -> a53;
    a03 -> O1;
    a13 -> O1;
    a23 -> O1;
    a33 -> O1;
    a43 -> O1;
    a53 -> O1;
    a03 -> O2;
    a13 -> O2;
    a23 -> O2;
    a33 -> O2;
    a43 -> O2;
    a53 -> O2;
    a03 -> O3;
    a13 -> O3;
    a23 -> O3;
    a33 -> O3;
    a43 -> O3;
    a53 -> O3;
    a03 -> O4;
    a13 -> O4;
    a23 -> O4;
    a33 -> O4;
    a43 -> O4;
    a53 -> O4;
}
有向图G{
rankdir=LR;
样条线=直线;
nodesep=“.05”;
edge[comment=“在例如中自动添加了通配符节点”];
节点[label=”“];
子图簇0{
颜色=白色;
label=“第1层(输入层)”;
edge[comment=“在例如中自动添加了通配符节点”];
节点[color=chartreuse,
样式=填充,
形状=圆];
x0[颜色=黄色,
fillcolor=黄色,
标签=];
x1[fillcolor=黄绿色,
标签=];
x2[fillcolor=黄绿色,
标签=];
x3[fillcolor=黄绿色,
标签=];
}
子图簇1{
颜色=白色;
label=“第2层(隐藏层)”;
edge[comment=“在例如中自动添加了通配符节点”];
节点[color=dodgerblue,
样式=填充,
形状=圆];
a02[颜色=黄色,
标签=,
pos=“0,0”,
填充颜色=黄色];
a12[标签=,
pos=“0,-1”,
fillcolor=道奇蓝];
a22[标签=,
pos=“0,-2”,
fillcolor=道奇蓝];
a32[标签=,
pos=“0,-3”,
fillcolor=道奇蓝];
a42[标签=,
pos=“0,-4”,
fillcolor=道奇蓝];
a52[标签=,
pos=“0,-5”,
fillcolor=道奇蓝];
}
子图簇2{
颜色=白色;
label=“第3层(隐藏层)”;
edge[comment=“在例如中自动添加了通配符节点”];
节点[color=dodgerblue,
样式=填充,
形状=圆];
a03[颜色=黄色,
fillcolor=黄色,
标签=];
a13[fillcolor=dodgerblue,
标签=];
a23[fillcolor=dodgerblue,
标签=];
a33[fillcolor=dodgerblue,
标签=];
a43[fillcolor=dodgerblue,
标签=];
a53[fillcolor=dodgerblue,
标签=];
}
子图簇3{
颜色=白色;
label=“第4层(输出层)”;
edge[comment=“在例如中自动添加了通配符节点”];
节点[color=coral1,
样式=填充,
形状=圆];
O1[fillcolor=coral1,
标签=];
O2[fillcolor=coral1,
标签=];
O3[fillcolor=coral1,
标签=];
O4[fillcolor=coral1,
标签=];
}
x0->a12;
x0->a22;
x0->a32;
x0->a42;
x0->a52;
x1->a12;
x1->a22;
x1->a32;
x1->a42;
x1->a52;
x2->a12;
x2->a22;
x2->a32;
x2->a42;
x2->a52;
x3->a12;
x3->a22;
x3->a32;
x3->a42;
x3->a52;
a02->a13;
a02->a23;
a02->a33;
a02->a43;
a02->a53;
a12->a13;
a12->a23;
a12->a33;
a12->a43;
a12->a53;
a22->a13;
a22->a23;
a22->a33;
a22->a43;
a22->a53;
a32->a13;
a32->a23;
a32->a33;
a32->a43;
a32->a53;
a42->a13;
a42->a23;
a42->a33;
a42->a43;
a42->a53;
a52->a13;
a52->a23;
a52->a33;
a52->a43;
a52->a53;
a03->O1;
a13->O1;
a23->O1;
a33->O1;
a43->O1;
a53->O1;
a03->O2;
a13->O2;
a23->O2;
a33->O2;
a43->O2;
a53->O2;
a03->O3;
a13->O3;
a23->O3;
a33->O3;
a43->O3;
a53->O3;
a03->O4;
a13->O4;
a23->O4;
a33->O4;
a43->O4;
a53->O4;
}

因此,我使用了一些肮脏的技巧来获得以下图片:

  • 我使用如下代码将所有节点连接在一起:

    x0->a02[样式=不可见,目录=无]

  • 我根据图片中显示的每个节点位置重新标记节点文本(这是真正脏的一个)

  • 因此,我的完整代码可在


    请告诉我是否有更好的解决方案。

    因此,我使用了一些肮脏的技巧来获得以下图片:

  • 我使用如下代码将所有节点连接在一起:

    x0->a02[样式=不可见,目录=无]

  • 我根据图片中显示的每个节点位置重新标记节点文本(这是真正脏的一个)

  • 因此,我的完整代码可在


    请告诉我是否有更好的解决方案。

    我认为更改标签不是一个好的解决方案

    如果每个层中节点的顺序很重要,我建议在每个层中添加不可见边,强制节点的顺序

    大概是这样的:

    digraph G {
        rankdir = LR;
        splines=false;
        edge[style=invis];
        ranksep= 1.4;
        {
            rank=same;
            x0->x1->x2->x3;
        }
        {
            rank=same;
            a0->a1->a2->a3->a4;
        }
        {
            rank=same;
            b0->b1->b2->b3->b4;
        }
        {
            rank=same;
            c0->c1->c2->c3;
        }
    
        edge[style=solid, tailport=e, headport=w];
        {x0; x1; x2; x3} -> {a0;a1;a2;a3;a4};
        {a0;a1;a2;a3;a4} -> {b0;b1;b2;b3;b4};
        {b0;b1;b2;b3;b4} -> {c0,c1,c2,c3};
    }
    

    我认为更换标签不是一个好的解决办法

    如果每个层中节点的顺序很重要,我建议在每个层中添加不可见边,强制节点的顺序

    大概是这样的:

    digraph G {
        rankdir = LR;
        splines=false;
        edge[style=invis];
        ranksep= 1.4;
        {
            rank=same;
            x0->x1->x2->x3;
        }
        {
            rank=same;
            a0->a1->a2->a3->a4;
        }
        {
            rank=same;
            b0->b1->b2->b3->b4;
        }
        {
            rank=same;
            c0->c1->c2->c3;
        }
    
        edge[style=solid, tailport=e, headport=w];
        {x0; x1; x2; x3} -> {a0;a1;a2;a3;a4};
        {a0;a1;a2;a3;a4} -> {b0;b1;b2;b3;b4};
        {b0;b1;b2;b3;b4} -> {c0,c1,c2,c3};
    }
    

    谢谢你的回答。我相应地修改了代码。整体效果相当好。然而,我所有的标签都不见了,图形变得倾斜。你有没有关于如何修理的建议?我的代码在这里:我稍微调整了一下。现在,除了图形倾斜外,一切正常。你知道怎么修吗?这是我修改过的代码:您可能需要添加一个不可见的边缘(
    a02->a03;
    )来防止倾斜。谢谢您的回答。我修改了我的c