Force the left to right order of nodes in graphviz?

社会主义新天地 提交于 2019-12-24 03:23:52

问题


I want to draw a decision tree chart using graphviz.

The graph I want to draw looks like this:

I am using the following dot language:

graph a {
  A [shape=box; label="A"]
  B [shape=box; label="B"]
  al [shape=none; label="0"]
  bl [shape=none; label="1"]
  br [shape=none; label="0"]

  A -- al [label="0"];
  A -- B [label="1"];
  B -- bl [label="0"];
  B -- br [label="1"];

}

However my resulting graph looks like this:

How can I force the left to right order of the nodes generated by graphviz? Furthermore, as far as decision trees go are these two trees exactly the same even though the left to right ordering is different?


回答1:


To fix this, you can just change the order of the B and al nodes:

graph a {
  A [shape=box; label="A"]
  al [shape=none; label="0"]
  B [shape=box; label="B"]
  bl [shape=none; label="1"]
  br [shape=none; label="0"]

  A -- al [label="0"];
  A -- B [label="1"];
  B -- bl [label="0"];
  B -- br [label="1"];
}

By the way, the graphviz website had a blog post about this problem which you can have a look at if you get a chance.




回答2:


Just note to anyone hitting this post because of "left to right" in the title. @marapet has an excellent answer to this post noting that order of appearance does not preserve layout. If you find that left-right layout is established at a certain point in the diagram, then broken by subsequent edges, try adding constraint=false to subsequent edges to prevent them from altering existing l-r layout.



来源:https://stackoverflow.com/questions/29864726/force-the-left-to-right-order-of-nodes-in-graphviz

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!