Give graphviz node layout information?

Hi all, newbie question here. My previous experience is with R and igraph, and now I’m using Python and NetworkX. Many resources seem to recommend using NetworkX for everything except the visualization, and to use Graphviz (Pygraphviz) for the latter.

I’d like to do this, but am facing one problem. I need to use the Kamada Kawai node layout algorithm, which NetworkX provides. However, as far as I can tell, Pygraphviz requires me to use one of its layout algorithms (neato|dot|twopi|circo|fdp|nop) when I call it. My question is: can I still take advantage of the drawing features that graphviz has to offer, and yet give it node layout coordinates from another algorithm?

Thanks,

  • Stephen

Yep, https://www.graphviz.org/faq/#FaqDotWithCoords

Besides, neato -Gmode=KK is Kamada-Kawai. The default now is the stress majorization solver (which is better) and fairly recently we added a stochastic-gradient descent solver which is also better (at least in terms of convergence). Also, it’s useful to know that Kamada-Kawai is stock multidimensional scaling from I guess the 1960s or 1970s.

Thanks Stephen (and Steve)!

A related n00b question is how to access these various layout options from Python. In your message, and in many posts, I see command-line calls to things like “neato”, with Linux-style command-line options like “-Gmode=KK”. But if I have an AGraph object in Python, and call “.draw()” on it, including “prog=neato” as an argument, how could I specify something like your “-Gmode=KK” option?

Also, let me clarify one thing: the reason I need (or think I need) Kamada-Kawai is that I’m creating multi-frame animations of a dynamic graph. In each frame, a small number of edges may appear or disappear, showing how the graph evolves over time. But in order for the animation to be coherent, I need the nodes to stay in roughly the same places from frame to frame. In R/igraph, the only way I could do this was to use Kamada-Kawai because it let me specify the previous iteration’s node coordinates as the “starting point” (or whatever it’s called) for KK to use in the next iteration. That worked nicely, and the nodes meandered slowly throughout the animation without constantly teleporting. Is there a better way to do this in networkx/pygraphviz? (It’s not that I’m in love with KK per se, just that I need a way to seed the layout engine with the previous iteration’s node coordinates.)

  • Stephen

Note that mode=KK does not have to be provided via command line. You can also provide it as a graph attribute, the same as layers or size or rankdir.
[I don’t use the python interface, but I’m pretty sure this is correct]

In most cases you can set graph attributes instead. -Gmode=KK is equivalent to mode=KK as an attribute within the graph body.

BTW, in graphviz, the SGD solver is said (by an author) to be very efficient in situations where many nodes are pinned and
only a few need to be moved, a case which does not work well in our implementation of KK.

Dynamic graph layout has been researched quite a bit, e.g.
https://www.semanticscholar.org/paper/Laplacian-based-dynamic-graph-visualization-Che-Liang/ab3bcdd22e7198bf813ccbadcd44d28559a5f69b
https://arxiv.org/pdf/2007.01229.pdf
but we do not have those algorithms in Graphviz. It would be nice to improve the interactive/dynamic aspects of it.