如何使用 python 在 graphviz 中定义内部子图?
How to define inner subgraphs in graphviz using python?
什么
是否可以使用 graphivz lib 在另一个内部编写子图?
我试过了
使用这个 API 我试图找到这个图像图表。它甚至来自另一个问题。也就是说,我知道这是可能的,用纯语言写作。但是,我需要使用 python 为状态机自动生成此图
图片:
我试过的一些代码
您可以使用嵌套的 with
子句。像这样:
from graphviz import Graph
g = Graph('G', filename="example.gv")
with g.subgraph(name="cluster_outside1") as outside1:
outside1.attr(label="outside 1")
with outside1.subgraph(name="cluster_inside1") as inside1:
inside1.attr(label="inside 1")
inside1.node("a", "a")
inside1.node("b", "b")
inside1.edge("a", "b")
with outside1.subgraph(name="cluster_inside2") as inside2:
inside2.attr(label="inside 2")
inside2.node("c", "c")
inside2.node("d", "d")
inside2.edge("c", "d")
with g.subgraph(name="cluster_outside2") as outside2:
outside2.attr(label="outside 2")
outside2.node("e", "e")
outside2.node("f", "f")
outside2.edge("e", "f")
g.view()
所以,你需要添加一个subgraph
调用父图的subgraph
方法
什么
是否可以使用 graphivz lib 在另一个内部编写子图?
我试过了
使用这个 API 我试图找到这个图像图表。它甚至来自另一个问题。也就是说,我知道这是可能的,用纯语言写作。但是,我需要使用 python 为状态机自动生成此图
图片:
我试过的一些代码
您可以使用嵌套的 with
子句。像这样:
from graphviz import Graph
g = Graph('G', filename="example.gv")
with g.subgraph(name="cluster_outside1") as outside1:
outside1.attr(label="outside 1")
with outside1.subgraph(name="cluster_inside1") as inside1:
inside1.attr(label="inside 1")
inside1.node("a", "a")
inside1.node("b", "b")
inside1.edge("a", "b")
with outside1.subgraph(name="cluster_inside2") as inside2:
inside2.attr(label="inside 2")
inside2.node("c", "c")
inside2.node("d", "d")
inside2.edge("c", "d")
with g.subgraph(name="cluster_outside2") as outside2:
outside2.attr(label="outside 2")
outside2.node("e", "e")
outside2.node("f", "f")
outside2.edge("e", "f")
g.view()
所以,你需要添加一个subgraph
调用父图的subgraph
方法