Specify or Change left and right child of a node in graphviz python

huangapple go评论61阅读模式
英文:

Specify or Change left and right child of a node in graphviz python

问题

I'm using Digraph from graphviz module in python to visualize a tree.
我正在使用Python中的graphviz模块中的Digraph来可视化树。

I create nodes by Digraph.node() and create edges by Digraph.edge().
我通过Digraph.node()创建节点,并通过Digraph.edge()创建边。

I want to specify which one should be right node and which one should be left node.
我想指定哪个应该是右节点,哪个应该是左节点。

But automatically it sets the first created node as the left child and the second created node as the right child.
但它自动将第一个创建的节点设置为左子节点,第二个创建的节点设置为右子节点。

For example in this code:
例如,在这个代码中:

from graphviz import Digraph

test = Digraph()

test.node('a')
test.node('b')
test.node('c')

test.edge('c', 'b')
test.edge('c', 'a')

test

Output is:

Specify or Change left and right child of a node in graphviz python

I want 'b' node to be the left child and 'a' node to be the right child like this:

Specify or Change left and right child of a node in graphviz python

I should emphasize I couldn't create 'b' node first and then create 'a' node.
我应该强调一下,我不能先创建'b'节点,然后再创建'a'节点。

Is there any way to do this for me with this library or not? If there isn't any solution for this module, recommend another module please.
有没有办法在这个库中为我做到这一点?如果这个模块没有解决方案,请推荐另一个模块。

英文:

I'm using Digraph from graphviz module in python to visualize a tree.
I create nodes by Digraph.node() and create edges by Digraph.edge().

I want to specify which one should be right node and which one should be left node.
But automatically it set first created node as left child and second created node as right child.

For example in this code:

from graphviz import Digraph

test = Digraph()

test.node('a')
test.node('b')
test.node('c')

test.edge('c', 'b')
test.edge('c', 'a')

test

Output is:

Specify or Change left and right child of a node in graphviz python

I want 'b' node be left child and 'a' node be right child like this:

Specify or Change left and right child of a node in graphviz python

I should emphasis I couldn't create 'b' node first then create 'a' node.

Is there any way to do this for me with this library or not? If there isn't any solution for this module, recommend another module please.

答案1

得分: 3

我将不翻译代码部分,以下是翻译好的内容:

"I will have to use a pipeline job. A pipeline is another type of job, which defines child jobs that may have input/output relationships, forming a directed acyclic graph (DAG). See the notes for SDKv2 migration for pipelines."

"我将不得不使用管道作业。管道是另一种类型的作业,它定义了可能具有输入/输出关系的子作业,形成有向无环图(DAG)。请参阅注释以了解有关管道的SDKv2迁移的详细信息。"

"The full sample is available from SDK v2 github repo along with other samples."

"完整示例2可从SDK v2 github存储库中获取,还提供了其他示例。"

英文:

I will have to use a pipeline job. A pipeline is another type of job, which defines child jobs that may have input/output relationships, forming a directed acyclic graph (DAG). See the notes for SDKv2 migration for pipelines.

The full sample is available from SDK v2 github repo along with other samples.

答案2

得分: 2

这是一个相当合理的愿望,但在python-to-graphviz接口(或Graphviz本身)中没有明确设置从上到下和从左到右定位的选项。然而,您可以通过在您的Python程序中进行一些“额外”的记录来实现您希望的输出。要么:

  • 在您的Python程序中创建一个堆栈/列表,其中包含您想要创建的,然后实际上以“反向”顺序创建它们。

或者

  • 以“自然”的顺序创建边,然后使用上面提供的链接中讨论的“不可见边”技巧,明确告诉Graphviz您想要的顺序。

第一种选择可能是开始的最简单的地方,甚至可能会导致一个更小、更干净但稍微更复杂的程序。祝你好运。

英文:

This is a quite reasonable desire, but there is nothing in the python-to-graphviz interface (or in Graphviz itself) that explicitly sets both top-to-bottom and left-to-right positioning.
However, you can probably get the output you desire by doing some "extra" bookkeeping in your python program. Either:

  • create a stack/list in your python program of the edges you want to create, but then actually create them in the "reverse" order

or

  • create the edges in the "natural" order, but then use the "invisible edge" trick that is discussed in the link given above to explicitly tell Graphviz what order you want.

The first alternative is probably the easiest place to start, and may even result in a smaller and cleaner, but slightly more complex, program. Good luck.

huangapple
  • 本文由 发表于 2023年5月11日 17:10:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76225941.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定