xflr6 / graphviz

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

Add rank #185

Closed AndiLeni closed 1 year ago

AndiLeni commented 1 year ago

Hello,

I am trying to add a rank to my graph. In detail, I want to add the two following lines to my graph, but I have no idea where such an option should be set.

 { rank=min; A; X; }
 { rank=max; B; D; Y; }

Thanks and kind regards

Full example:

digraph G { 

  rankdir = TB;

  A -> B;
  A -> C -> D;
  X -> Y;

  { rank=min; A; X; }
  { rank=max; B; D; Y; }

}
xflr6 commented 1 year ago

AFAICT you want to replicate something very similar to https://stackoverflow.com/questions/25734244/how-do-i-place-nodes-on-the-same-level-in-dot. Did you check the documentation or the examples (e.g. searching for rank)?

In [1]: import graphviz
   ...: 
   ...: g = graphviz.Digraph(name='G')
   ...: 
   ...: g.attr(rankdir='TB')
   ...: 
   ...: with g.subgraph() as s:
   ...:     s.attr(rank='min')
   ...:     s.node('A')
   ...:     s.node('X')
   ...: 
   ...: with g.subgraph() as s:
   ...:     s.attr(rank='max')
   ...:     s.node('B')
   ...:     s.node('D')
   ...:     s.node('Y')
   ...: 
   ...: g.edge('A', 'B')
   ...: g.edge('A', 'C')
   ...: g.edge('C', 'D')
   ...: g.edge('X', 'Y')
   ...: 
   ...: print(g)
   ...: g
digraph G {
    rankdir=TB
    {
        rank=min
        A
        X
    }
    {
        rank=max
        B
        D
        Y
    }
    A -> B
    A -> C
    C -> D
    X -> Y
}

Out[1]: 

spam

AndiLeni commented 1 year ago

Thanks for the hint. It was an issue with graphviz and not related to this python module.

I used subgraphs with min and max, but those are not exclusive. In the graphviz forum I was told to use source and sink instead, which works fine.

Thanks nonetheless and kind regards