固定节点
graphviz中结点的位置是自动生成的,添加边会影响节点的位置。如果要固定节点位置,在添加边时需要设置constraint='false'
。
代码
def link_edges(d, starts, ends):
for i_idx,i in enumerate(starts):
for j_idx,j in enumerate(ends):
if i_idx==j_idx:#print(i,j)
d.edge(i, j,taillabel='0.24',fontsize='9',labeldistance='2',constraint='true')#,label=i+j)
else:
d.edge(i, j,taillabel='0.24',fontsize='9',labeldistance='2',constraint='false')#,label=i+j)
d = graphviz.Digraph()
# penwidth 0 ,去掉子图边框
d.attr(rankdir='LR', penwidth='0',splines='line',size='6,6',nodesep='0.8')
d.attr('node', shape='circle', style='filled',fixedsize='true')
with d.subgraph(name='cluster_2') as a:
a.attr(label='output layer',rank='same')
a.node_attr.update(fillcolor='cadetblue1')
a.node('o1')
a.node('o2')
a.node('o3')
with d.subgraph(name='cluster_1') as b:
b.attr(label='hidden layer',rank='same')
b.node_attr.update(fillcolor='cadetblue1')
b.node('h1')
b.node('h2')
b.node('h3')
with d.subgraph(name='cluster_0') as c:
c.attr(label='input layer',rank='same')
c.node_attr.update(fillcolor='darkseagreen1')
c.node('i1')
c.node('i2')
c.node('i3')
link_edges(d,['i1','i2','i3'],['h1','h2','h3'])
link_edges(d,['h1','h2','h3'],['o1','o2','o3'])
d