How to get the coordinates from layout from graphviz?

You can add the layout information into the graph with :

gv.render(gvv)

and then find out the position of a node getting its attribute pos :

n_france = gv.findnode(gvv, "France")
pos = gv.getv(n_france, "pos")

Then depending on what you want to do, you might need to convert dot coordinates into png image coordinates. You can get useful information from here :

http://www.graphviz.org/faq/#FaqCoordTransformation

It explains in great details the computation from the graph-units to image pixels.

Hope that this is what you are looking for.


I just found a similar solution that works perfectly for my needs

pos = nx.drawing.nx_agraph.graphviz_layout(G, prog='dot', args='-Grankdir=LR')

cheers!


Using pydotplus you can load in and parse a dot/gv file and interrogate the data structure pydotplus produces, but this internal representation seems not to initially possess all the node attributes, like pos, unless they were already in the file.
But you can also call .write_dot() to produce a much more verbose dot file version. If you parse this then the resulting data structure seems to have pos of all the nodes (and even pos for the splines)

Note: maybe best to index the nodes by name not by index because any text with square brackets after it in the verbose file will be parsed as a node, so the node list may have spurious extra elements.

In the following (slightly edited) experiment at the Spyder prompt I have a terse dot file interior.gv (that does not have pos for nodes) which I .graph_from_dot_file(), then .write_dot(). Then .graph_from_dot_file() again on the verbose generated file, and so find the pos as required.

import pydotplus as pdp

interior = pdp.graphviz.graph_from_dot_file('interior.gv')

interior.write_dot('interior2.dot')
Out[210]: True

interior2 = pdp.graphviz.graph_from_dot_file('interior2.dot')

interior2.get_nodes()[3].get_pos()
Out[214]: '"213.74,130"'