我是pydot的新手,我将用它来创建图表。我想格式化节点,以便在同一行中有节点块。我使用子图完成了,并将等级设置为相同。
import pydot
# define a graph
graph = pydot.Dot(graph_type="digraph")
# define nodes
node_rec = pydot.Node(label = "Start",shape = "box")
node_bs = pydot.Node("bs")
node_man = pydot.Node("man")
node_dec = pydot.Node("dec")
node_ms = pydot.Node("ms")
node_gtp = pydot.Node("gtp")
node_des = pydot.Node("End",shape = "box")
# add nodes
graph.add_node(node_rec)
graph.add_node(node_man)
graph.add_node(node_gtp)
graph.add_node(node_des)
# add edges
graph.add_edge(pydot.Edge(node_rec, node_bs))
graph.add_edge(pydot.Edge(node_rec, node_man))
graph.add_edge(pydot.Edge(node_bs, node_dec))
graph.add_edge(pydot.Edge(node_dec, node_ms))
graph.add_edge(pydot.Edge(node_ms, node_gtp))
graph.add_edge(pydot.Edge(node_gtp, node_man))
graph.add_edge(pydot.Edge(node_man, node_des))
# create a subgraph
layer1 = pydot.Subgraph(rank='same')
layer1.add_node(node_bs)
layer1.add_node(node_dec)
layer1.add_node(node_ms)
graph.add_subgraph(layer1)
# create figure
graph.write_png('output_ok.png')
但是,如果我想要有两个子图,一个在彼此之上,我无法掌握什么是正确的代码。这里是一个使用相同节点的示例,构建了两个具有不同等级的子图,但它失败了。
# add nodes
graph.add_node(node_rec)
graph.add_node(node_des)
# add edges
graph.add_edge(pydot.Edge(node_rec, node_bs))
graph.add_edge(pydot.Edge(node_rec, node_man))
graph.add_edge(pydot.Edge(node_bs, node_dec))
graph.add_edge(pydot.Edge(node_dec, node_ms))
graph.add_edge(pydot.Edge(node_ms, node_gtp))
graph.add_edge(pydot.Edge(node_gtp, node_man))
graph.add_edge(pydot.Edge(node_man, node_des))
layer1 = pydot.Subgraph(rank='source')
layer1.add_node(node_bs)
layer1.add_node(node_dec)
layer1.add_node(node_ms)
graph.add_subgraph(layer1)
layer2 = pydot.Subgraph(rank='max')
layer2.add_node(node_man)
layer2.add_node(node_gtp)
graph.add_subgraph(layer2)
# create figure
graph.write_png('output_bad2.png')
我知道我错了吗?