Skip to main content
 首页 » 编程设计

python之Graphviz:Python 中记录标签的边 (gv)

2024年10月25日12xxx_UU

我很难产生以下优势:

... 
h1 -> "h2":f2; 

如本例所示:datastruct

考虑以下 Python 代码:

#!/usr/bin/env python 
 
import sys 
import gv 
 
gh = gv.digraph('g') 
 
gv.setv(gh, 'rankdir', "LR") 
 
node = gv.protonode(gh) 
gv.setv(node, 'shape', 'record') 
 
n1 = gv.node(gh, "h1") 
 
n2 = gv.node(gh, "h2") 
gv.setv(n2, "label", "<f1> s1 | <f2> s2") 
 
e = gv.edge(n1, n2) 
 
gv.write(gh, "out.dot") 

执行后输出如下:

digraph g { 
    node [label="\N"]; 
    graph [rankdir=LR]; 
    h1 [shape=record]; 
    h2 [label="<f1> s1 | <f2> s2", shape=record]; 
    h1 -> h2; 
} 

这给我留下了一个问题,如何获得上面显示的边,以便它指向正确的记录条目。

提前致谢。

请您参考如下方法:

您需要设置 headport 才能做到这一点:

我只用 C 语言做过这个,但是从你在 python 中展示的内容我认为是这样的:

e = gv.edge(n1, n2) 
gv.setv(e, "headport", "f1") # or another key from the records 

这将创建这样的边缘:

h1 -> h2:f1; 

如果需要指向反方向,也可以设置tailport。