使用 graphviz 将节点定位到子图的右侧

Position a node to the right of a subgraph with graphviz

我正在使用 graphviz 绘制简单潜在变量模型的 DAG:

digraph G {

    splines=line;

    subgraph cluster {
        node [style=filled, shape=circle];
        edge [color=blue]
        z[fillcolor=white, color=black, pos = "0,0!"]
        z -> x;
    }

    theta[label = "θ", shape=circle, pos = "10,0!"]
    edge [color=black, style="dashed"]
    theta->z
    theta->x

}

输出还不错:

但我希望 θ 与 z 处于同一高度。那可能吗?我尝试使用 pos 属性,但如您所见,它被很好地忽略了。我在 HackMD.

工作

您可以将 constraint=false 属性添加到您的 theta 边缘,这样它们就不会影响布局并且节点将保持并排:

digraph G {

    splines=line;

    subgraph cluster1 {
        node [style=filled, shape=circle];
        edge [color=blue]
        z[fillcolor=white, color=black, pos = "0,0!"]
        z -> x;
    }
    theta[label = "θ", shape=circle, pos = "10,0!"]
    edge [color=black, style="dashed"]
    theta -> z [constraint=false]
    theta -> x [constraint=false] // actually this one is unnecessary, may be omited in this example

}

此外,您可以尝试边缘方向(例如改变位置 a->b,b->a),这有时有助于定位簇。