1

I have the following dictionary with edges as keys and floats as values.

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}

And what I call a base, which is really just the nodes involved, ignoring i and f. base = set([24,25,26,27,29,30,33,34,35,36,37]). I want to plot a Digraph using graphviz. I did it as follows:

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()

However, I don't get the expected result.

enter image description here

I would like a top-bottom digraph with node i on top, on a second level nodes 24 and 25, then nodes 26 and 27, then the rest of the nodes, and finally node f at the bottom.

I have read multiple posts and tried using the attribute rank with no luck. Any suggestions?

YamiOmar88
  • 1,336
  • 1
  • 8
  • 20

0 Answers0