graphviz + Python:如何分配节点位置?

时间:2017-07-10 13:13:15

标签: python graphviz digraphs

我有以下字典,边缘为键,浮点数为值。

edges = {(24, 26): 0.513, (24, 27): 0.185, (25, 26): 0.205, (25, 27): 0.096, (26, 29): 0.561, (26, 30): 0.158, (27, 29): 0.217, (27, 30): 0.064,` (29, 30): 0.761, (29, 33): 0.202, (29, 34): 0.027, (29, 35): 0.002, (29, 36): 0.001, (29, 37): 0.007, (30, 29): 0.222, (30, 33): 0.613, (30, 34): 0.114, (30, 35): 0.012, (30, 36): 0.009, (30, 37): 0.031, (33, 30): 0.014, (33, 34): 0.823, (33, 35): 0.024, (33, 36): 0.035, (33, 37): 0.054, (33, 'f'): 0.05, (34, 30): 0.002, (34, 33): 0.061, (34, 35): 0.43, (34, 36): 0.426, (34, 37): 0.066, (34, 'f'): 0.015, (35, 33): 0.024, (35, 34): 0.002, (35, 37): 0.412, (35, 'f'): 0.053, (36, 33): 0.015, (36, 34): 0.002, (36, 37): 0.43, (36, 'f'): 0.061, (37, 33): 0.086, (37, 34): 0.032, (37, 35): 0.024, (37, 36): 0.038, (37, 'f'): 0.821, ('i', 24): 0.699, ('i', 25): 0.301}

我称之为base,实际上只是涉及的节点,忽略了ifbase = set([24,25,26,27,29,30,33,34,35,36,37])。 我想用graphviz绘制一个有向图。我这样做了:

from graphviz import Digraph    
g = Digraph('G', filename='myFile.gv')
lwdM = max(edges.values())
for k,v in edges.items():
    pwdt = str(10.0 * v / lwdM)
    g.edge(str(k[0]), str(k[1]), penwidth=pwdt)

g.attr('node', rankdir='TB')
g.node('i', shape='diamond', style='filled', color='lightblue')
g.node('f', shape='diamond', style='filled', color='lightblue')

line0 = {i:'red' for i in range(24)}
line1 = {i:'green' for i in [24,25]}
line2 = {i:'blue' for i in [26,27,28]}
line3 = {i:'yellow' for i in range(29,52)}
colors = {k: v for d in [line0, line1, line2, line3] for k, v in d.items()}

for n in base:
    myCol = colors[n]
    n = str(n)
    g.node(n, shape='ellipse', style='filled', color=myCol)

g.view()

但是,我没有得到预期的结果。

enter image description here

我想在顶部,第二级节点i24上添加节点25的顶部有向图,然后是节点26和{{1} },然后是其余节点,最后是节点27在底部。

我已阅读多篇帖子并尝试使用属性f但没有运气。有什么建议吗?

0 个答案:

没有答案