Graphviz family tree with fixed nodes - graphviz

I just had the idea to overlay a family tree over a table plan such that guests of my event can see where other family members sit. I have the table plan as pdf file. Would it be possible to fix the positions of the nodes such that they coincide with the persons seat and to let graphviz optimize the routing of the edges?

You can certainly fix the node positions (using the pos attribute with ! or the node attribute pin=true -- see the answer to graphviz - fixed node positions, which appears to be by one of the GraphViz team members, however the edge routing might require some aesthetic help.
For example, given familytree.dot:
digraph G {
node [ shape="plaintext" ]
graph [ splines="ortho" ]
dad -> parents
mom -> parents
parents -> son1
parents -> son2
parents -> daughter1
parents -> daughter2
granddad -> dad
granddad [pos = "0,2!"]
dad [pos = "0,4!"]
mom [pos = "0,6!"]
son1 [pos = "2,2!"]
son2 [pos = "2,4!"]
daughter1 [pos = "2,6!"]
daughter2 [pos = "2,8!"]
}
... running neato -Tpng -ofamilytree.png familytree.dot gives the following result:

Related

In GraphViz, in a strict graph, is it possible to have 2 edges between nodes if they have different attributes?

What I want to achieve is, if I have something like
Strict Digraph G {
a -> b [label = edge1]
a -> b [label = edge2]
a -> b
a -> b
a -> b [label = edge1]
}
then I should get a graph with 2 nodes and 3 edges between them (One with label edge1, one with edge2 and one without a label).
It doesn't seem to be possible, with the strict keyword with this input, it only draws one edge.
Sorry, not "legal". From https://graphviz.org/doc/info/lang.html
A graph may also be described as strict. This forbids the creation of multi-edges, i.e., there can be at most one edge with a given tail node and head node in the directed case.

Graphviz: Edge orientation to and from same node

I'm creating some multilevel SEM graphs and I'm running into a small problem. The latent variable at the bottom of the graph (labeled "WF") is supposed to have a double-headed edge to and from it but the edge should go under the node. Note the correct orientation of the topmost node. Seems and easy fix but I can find it. Thanks in advance. (BTW, I've provided only a snippet of the original model, but this is sufficient for the purpose.)
digraph stackex {
{rank=min;
bf [shape=ellipse]
bf:nw -> bf:ne [dir = both]}
{node[shape=square]
bf -> i1
i1[label=X1]
i1 -> wf [dir=back]}
{wf [shape=ellipse]
wf:sw -> wf:se [dir = both]}
}
And here's what it produces:
The double-headed arrow should go under the node labled "WF".
Based on experimentation, it appears that the only way to get
wf:sw -> wf:se [dir = both]
to do what you want is to add
graph [rankdir=TB]
Unfortunately, rankdir affects the entire graph (not just a subgraph or cluster). So you can fix one loop, but you break the other.
The only way I've found to accomplish your goal is to hand-modify the pos of the offending edge (spline). This:
digraph stackex {
graph [bb="0,0,82.583,216"];
node [label="\N",
shape=square
];
bf [height=0.5,
pos="41.292,162",
shape=ellipse,
width=0.75];
bf:nw -> bf:ne [dir=both,
pos="s,26.292,177 e,56.292,177 22.277,186.17 18.135,200.16 21.792,216 41.292,216 60.792,216 64.448,200.16 60.306,186.17"];
i1 [height=0.5,
label=X1,
pos="41.292,90",
width=0.5];
bf -> i1 [pos="e,41.292,108.1 41.292,143.7 41.292,135.98 41.292,126.71 41.292,118.11"];
wf [height=0.5,
pos="41.292,18",
shape=ellipse,
width=0.75];
i1 -> wf [dir=back,
pos="s,41.292,71.697 41.292,61.665 41.292,53.054 41.292,43.791 41.292,36.104"];
wf:se -> wf:sw [dir=both,
pos="s,56.292,3 e,26.292,3
65.002,8.3185
92.908,0.389
88.823,-20
41.292,-20
-6.2395,-20
-10.324,0.389
17.582,8.3185"];
}
And this command
neato -n2 -Tpng doubleheaded3.fixed.dot >doubleheaded3.fixed.png
Gives this:
All in all, I'd suggest the pic/gpic/dpic language. Lower-level, but probably easier to use in the long run.

Node positions in Graphviz

I'm trying to set up the following graph so that the 'old_view' & 'new_views' are at the same rank at the top, and 'old_submits & 'new_sub' are on the same rank at the bottom. 'Continues' would sit in between the two rows.
I've tried using subgraphs as suggested by others but it hasn't helped me on this one.
c=Digraph('parent')
c.attr('graph', label='')
c.attr('node',fontname='helvetica')
c.attr('graph',fontname='helvetica')
c.attr('edge',fontname='helvetica')
c.attr('node', shape='box', color='lightgrey')
c.attr(rank='same')
c.node('old_views')
c.node('new_views')
c.node('continues')
g=Digraph('subgraph')
g.graph_attr.update(rank='same')
g.node('new_submits')
g.node('old_submits')
c.edge('new_views','continues')
c.edge('continues','new_submits')
c.edge('old_views','old_submits')
c.subgraph(g)
c
Example image:
A pure graphviz hint that you should be able to transfer to python easily:
If you want to "skip" a level, you have two possibilities (at least):
You can use an empty node - downside is that you have to define that empty node b, and that your edge pointing from a to b needs to have no arrowhead. Also, if you look carefully, you seen an empty pixel on the way from a to c.
The latter you can avoid by routing an extra invisible edge from A over E to C, creating not only the need for this extra edge but also for an increased weight on theD -> E -> E edge to keep it straight.
digraph so
{
b[ shape = point, width = 0]
a -> b[ dir = none ];
b -> c;
d -> e -> f;
A -> C;
A -> E -> C[ style = invis ];
D -> E -> F[ weight = 10 ];
}
The choice is yours!

simple "T shaped" graph in graphviz

Need draw a graph with dot/graphviz like this image:
The texts can be above arrows, like graphviz does it. But how to achieve the T-layout? Need make a cluster for the top row?
This is one possibility using rank=same for a subgraph:
digraph g {
node[shape=point, width=0.2];
{
rank=same;
p1 -> n [label="text1"];
n -> p2 [label="text2"];
}
n -> p3 [label="text3", dir=back];
n[label="node", shape=rect, style=rounded];
}
You could also use a left-right layout instead of top-down.
An other possibility is to disable the effect of some edges using constraint=false:
digraph g {
node[shape=point, width=0.2];
p1 -> n [label="text1", constraint=false];
n -> p2 [label="text2", constraint=false];
n -> p3 [label="text3", dir=back];
n[label="node", shape=rect, style=rounded];
}
The result is the same.
dot usually layouts trees in layers. To force an edge to not be a layer separation you can add the constraint=false option. So something like:
digraph {
A [shape=point]
B [shape=point]
C [shape=point]
N [label="node"]
A -> N [label="text1", constraint=false]
N -> B [label="text2", constraint=false]
N -> C [label="text3", dir=back]
}
should work.
Note that the edge from the lower node to "node" has to be backwards, since dot layouts trees from top to bottom. Therefore the logical edge direction has to be from top to bottom, even though the display direction might be the other way round (which is the case here).

Forcing "main line" nodes into a straight line in Graphviz (or alternatives)

I'm trying to use Graphviz dot (but am willing to use something else) to generate a graph with a long "main line" of nodes, and many small branches. I'd like the main line to be straight from left to right, with the small branches above or below it. However, Graphviz "balances" the two branches, so I end up with a crooked graph.
To illustrate, here's a sketch similar to what I currently get:
And this is what I actually want:
Is there any way to force or encourage Graphviz to generate a graph like the second one? I may be able to use "dummy" second branches to have it do a 3-way layout, and then hide/delete the dummies afterward, but if there's a better option that would be preferable.
Here is a solution using the weight attribute of edges:
digraph G {
rankdir="LR";
node[width=0.15, height=0.15, shape=point];
edge[weight=2, arrowhead=none];
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8;
edge[weight=1];
2 -> 9 -> 10 ;
5-> 11 -> 12;
}
Result:
A second solution using the group attribute of nodes - again a directed graph:
From the graphviz dot reference:
If the end points of an edge belong to
the same group, i.e., have the same
group attribute, parameters are set to
avoid crossings and keep the edges
straight.
So here we go:
digraph g{
rankdir="LR";
node[width=0.15, height=0.15, shape=point, group=main];
edge[arrowhead=none];
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8;
node[group=branches];
2 -> 9 -> 10;
5 -> 11 -> 12;
}
Output is exactly the same as in my first answer.

Resources