In a graphviz dot digraph, how can I break a wide layout (rankdir LR)

Viewed 1760

With python I'm trying to generate a long graph where always one node points to the next. This ends up in having a long snail of nodes (rankdir LR). However I want to break it after a certain width or number or nodes. How can this be achived?

graph = gv.Digraph(format='svg')
graph.graph_attr.update({'rankdir': 'LR'})

graph.node('a', 'A')
graph.node('b', 'B')
graph.node('c', 'C')
graph.node('d', 'D')
graph.node('e', 'E')
graph.node('f', 'F')
...

graph.edges(['ab', 'bc', 'cd', 'de', 'ef', ...])

Output:

all items in one row

However I want (or similar):

items arranged in two rows with keeping the connection

I tried to use size, but that only zooms the whole graph.

As a workarround I tried to reduce ranksep, but that only makes it better for a few more items.

I also searched a lot but could not find an appropriate answer. An unanswered question that goes into a similar direction is: graphviz plot too wide. For other related questions suggested answer was to use invisible elements but that does not work here either.


Update: I've altered the code for edges according to the comment of @vaettchen:

graph.edge('a', 'b', None, {'weight':'5'})
graph.edge('b', 'c', None, {'weight':'5'})
graph.edge('d', 'e', None, {'weight':'5'})
graph.edge('e', 'f', None, {'weight':'5'})

graph.edge('c', 'd', None, {'weight':'1'})
graph.edge('a', 'd', None, {'style':'dashed', 'rank':'same'})

Unfortunately the result now looks like this (style 'dashed' instead of 'invis' for better visibility): enter image description here

'rank': 'same' seems not change anything. Also when applied to nodes A and D.

1 Answers

This should be a comment rather than an answer as it doesn't address the python issue and I guess you are also looking for something more "automatic" - but maybe it gives some ideas; and as nobody else is picking it up, here a pure graphviz suggestion:

digraph so 
{
    // graph attributes
    rankdir = LR;       // horizontal graph
    splines = ortho     // edges with "corners"

    // default/initial node style
    node[ shape = box ];

    // nodes where the "new lines" begin
    // connected invisibly to keep them in order
    { rank = same; A ->  E ->  I[ style = invis ] }

    // nodes that are to be in one line
    // extra weight needed to keep the edges straight
    edge[ weight = 5 ];
    A -> B -> C -> D;
    E -> F -> G -> H;
    I -> J -> K -> etc;

    // edges connecting the graph elements over the lines
    edge[ weight = 1 ];
    D -> E;
    H -> I;
}

yields

enter image description here

Related