3

I am new to graphviz and am just wondering how to determine the relative node positioning in a graph. For example, if I want to draw a simple triangle 'abc', with node 'a' being at the top and nodes 'b' and 'c' on the same level at the bottom, how should I tell graphviz to lay out the nodes as desired?

I tried the following:

graph G
{
   node [shape="circle"];
   edge [lblstyle="auto"];

   {rank=min; "a"}
   a -- b [label = "-"];
   a -- c [label = "-"];
   {rank=same; "b" "c"}
   b -- c [label = "+"];
}

but the output positions nodes 'a' and 'b' on the same level at the top, with node 'c' at the bottom.

In addition, is it possible to draw two such triangles side-by-side (with a nice appropriate space in between) in the same graph? if so, how is it implemented?

Thanks a lot.

marapet
  • 54,856
  • 12
  • 170
  • 184
skyork
  • 7,113
  • 18
  • 63
  • 103

1 Answers1

6

but the output positions nodes 'a' and 'b' on the same level at the top, with node 'c' at the bottom.

I actually get a on top, centered above b and c (see image).

Your markup, slightly simplified (what's lblstyle ?), seems to achieve what you want when rendered with dot:

graph G
{
   node[shape=circle];

   a -- b [label = "-"];
   a -- c [label = "-"];
   {rank=same; b -- c [label="+"];}
}

graphviz triangle FTW

What version of graphviz do you use?

And to have two triangles side by side:

graph G
{
   node[shape=circle];
   edge[label="-"];


   a -- b;
   a -- c;
   {rank=same; b -- c [label="+"];}

   d -- e;
   d -- f;
   {rank=same; e -- f [label="+"];}
}

However, if things get more complex, it may be difficult to have graphviz layout everything exactly as one would like. That's actually the strength of graphviz - applying layout algorithms in order to not have a user intervene.

marapet
  • 54,856
  • 12
  • 170
  • 184
  • i'm actually using dot2tex package: http://www.fauskes.net/code/dot2tex/documentation/. I copied your construction above into a LaTeX doc, and it does not produce the same graph as you are able to obtain, instead, it gives me the one that is described in my original post. – skyork Jul 18 '11 at 21:19
  • That explains the lblstyle - sorry, I don't have any experience with that package. Nevermind my answer... – marapet Jul 18 '11 at 21:37
  • The `{rank=same; e -- f [label="+"];}` worked for me, in a Confluence Flowchart macro. – Mike Finch Feb 02 '22 at 03:18