How to create double labelled edges in Graphviz? - graphviz

I would like to create more than one label for one edge. The result I'm interested in looks like this:
Note that the edge {0,2} has two labels: e and z. One is above the edge, the other one - below. This is the exact outcome I want. How can I achieve this?

Have you tried
digraph G {
graph [ splines = false rankdir="LR" ]
a -> b [ label = "foo" ];
a -> b [ label = "bar" ];
}
I've checked it using http://www.webgraphviz.com/
Result looks like
Taken from stackoverflow question Graphviz, grouping the same edges

Related

In GraphViz, make node positions within a subgraph independent from nodes in other subgraphs?

Using dot, the basic layout puts nodes into layers. If you create subgraphs, it groups related nodes inside of rectangles, but the nodes are still in layers, and those layers are influenced by the nodes outside of the subgraph.
Sometimes, this is great. But sometimes, when a subgraph is an independent visual entity, it might be nice to be able to lay out its content without respect to the layers of the other parts of the graph. Take for example the following:
digraph x {
subgraph one {
a [ label="a\nvery\nlong\nlabel" ]
b [ label="another\nvery\nlong\nlabel" ]
c [ label="still\nmore\nlong\nlabels" ]
a -> b - > c
}
subgraph two {
w -> x -> y -> z
}
}
Because of the long labels, the nodes in subgraph one will take up a lot of space. But because of the layer-based layout, the nodes in subgraph two will be vertically aligned with the corresponding nodes from subgraph one.
Is there a way to make it layout subgraph two as if subgraph one did not exist?
Not directly with dot. Dot does rank alignment across the entire graph. However here are two ways to get close to what you want:
use neato -Goverlap=false -Gmode=hier to approximate a dot layout:
or split the various parts into separate graphs, use dot -Tdot to layout each, then use gvpack (https://graphviz.org/pdf/gvpack.1.pdf) to combine the parts into a single output. Like so:
dot -Tdot ComboGraph1a.gv >ComboGraph1a.dot
dot -Tdot ComboGraph1b.gv >ComboGraph1b.dot
gvpack -array_ib2 ComboGraph1?.dot |neato -Tpng -n2 >oooo.png
Giving:

Edge labels writing over edge arrows in Graphviz

For my decision tree I need to place edge label nodes next to the nodes in GraphViz for readability, and i have tried to use taillabel but it writes over the edge arrow.
For example the following code:
digraph workflow
{
checkit -> doit [taillabel ="y";];
checkit -> dontdoit [taillabel="n";];
}
renders like this (I use GVedit : Graphviz 2.38.0, Graphvizversion 1.02) :
How can I ensure that edge labels such as 'y' do not write over the edge arrow ?
You can use labelangle and labeldistance attribute to control the precise position of taillabel or headlabel:
digraph workflow {
checkit -> doit [
taillabel ="y"
labeldistance=2
labelangle=330
]
checkit -> dontdoit [
taillabel="n"
labeldistance=2
labelangle=25
]
}

Is it possible to place a label midway of an edge in graphviz?

Is is possible to make a graph like this:
Where the label is in the path of an edge between two nodes.
The closest you get, as far as I know, is rather than trying to fiddle around with edge lables, using text only nodes:
digraph so
{
// standard nodes
node[ shape = box ];
A[ label = "Animal" ];
B[ label = "Elephant" ];
// fake label nodes
node[ shape = none ];
a[ label = " large" ];
// get them connected
edge[ arrowhead = none ];
A -> a -> B;
}
This gives you
This is not optimal as it gives you layout problems - the distance between your "real" nodes is probably larger than you want, and you would have to introduce empty nodes for edges without labels. But at least it comes close to the picture you have posted.

Can you join edges when they go to the same node?

See the line on the leftmost side of this image.
This isn't a perfect example because the lines don't end on a node, but imagine that there is a node on the bottom left corner of the image. Normally in graphviz if I have a graph like this:
digraph G {
a->c
b->c
}
Then I get two separate lines going into c. Is it possible to have these two lines join before they reach c?
Yes, it is possible to have two lines join before they reach c but, as far as I can see, it requires inserting an invisible node to do it. e.g.:
digraph G {
x [style=invis, height=0, label=""]
a->x [dir=none]
b->x [dir=none]
x->c
}
... which gives:

edge-layout in graphviz for fixed node positions

I was trying to write my own little algorithm for graph layout that only creates a node layout but does not define edge routes. When I use Graphviz to turn the resulting dot file into a graph, the edges are straight lines that cross the nodes and even overlap each other. Is there a way to use Graphviz to layout the edges as nicely as the dot algorithm does, but have the nodes in predetermined fixed positions?
You can see the effect for instance on the following graph:
digraph test {
"a" [pos="0.0,0.0"];
"b" [pos="50.0,50.0"];
"c" [pos="100.0,100.0"];
"a" -> "b";
"a" -> "c";
"b" -> "c";
}
When drawn with dot -Knop -Tpng -otest.png test.dotty the line between a and c crosses b. What I want is that all nodes keep their positions, but the line between a and c goes around b.
Just add:
splines=true;
to your graph - result is:

Resources