xflr6 / graphviz

Simple Python interface for Graphviz
https://graphviz.readthedocs.io
MIT License
1.63k stars 211 forks source link

Support for {rank: <rank>; [<node_id>;]+} #154

Closed cpopescu closed 2 years ago

cpopescu commented 2 years ago

Sometimes is useful to enforce the same rank on a group of nodes (or subgraphs) using something like the structure described in the subject. E.g. compare

digraph G {
  a; b; c; d;
  a -> b; b->c; a->d;
}

with

digraph G {
  a; b; c; d;
  a -> b; b->c; a->d;
  { rank = same; a; b }
}

I couldn't find how to do it with current API - Is it possible to add support for that? (e.g. dot.rank('same', ['a', 'b']) or similar)

Thanks a lot !

cp

xflr6 commented 2 years ago

Does https://graphviz.readthedocs.io/en/stable/examples.html#rank-same-py help (mentioned in attributes docs)?

xflr6 commented 2 years ago
import graphviz

dot = graphviz.Digraph()

with dot.subgraph() as s:
    s.attr(rank='same')
    s.node('a')
    s.node('b')

dot.node('c')
dot.node('d')

dot.edges(['ab', 'bc', 'ad'])

dot

spam