graphviz python How to not align nodes from cluster with nodes from another cluster

299 views Asked by At

I have 4 vertical clusters with 4 nodes inside: one node wit a random number of 0s, one node with a random number of 1s, etc.

My problem is that each nth node inside the cluster does a central alignment with the nth node inside the other clusters:

enter image description here

I'd prefer to have each cluster start on the same line and then stack the nodes at the top of the cluster instead of having them trying to align with nodes in the other clusters... any idea how to do that ?

Here is my code to generate the tree:

from random import randrange
from graphviz import Digraph

dot = Digraph(comment='XR IC Tree', node_attr={'shape': 'record'})
dot.attr(compound='true')
dot.attr(rankdir='TB')
dot.node('Root', 'Root')
for i in range(2):
    dot.node(str(i), str(i))
    dot.edge("Root", str(i))
    for j in range(0, 2):
        dot.node(str(i) + str(j), str(i) + str(j))
        dot.edge(str(i), str(i) + str(j))
        with dot.subgraph(name="cluster_" + str(i) + str(j), node_attr={'shape': 'record'}) as a:
            a.attr(rankdir='TB')
            for k in range(0, 4):
                n = str(i) + str(j) + str(k)
                m = "{}".format(k) + "| {}".format(k) * randrange(10)
                a.node(n, "{{ {} }}".format(m))
                if k:
                    dot.edge(str(i) + str(j) + str(k-1), str(i) + str(j) + str(k), style="invis")
            dot.edge(str(i) + str(j), str(i) + str(j) + str(0), lhead="cluster_" + str(i) + str(j))

dot.render('test1', format="jpg")

Thanks !

0

There are 0 answers