21
$\begingroup$

Given a Graph with an automatically computed layout (i.e. not explicitly given VertexCoordinates, but using a GraphLayout method), how can we extract the coordinates of the vertices?

In[]:= g = RandomGraph[{10, 20}, GraphLayout -> "SpringEmbedding"]
Out[]= << picture of graph >>

In[]:= PropertyValue[g, VertexCoordinates]
Out[]= Automatic (* <-- I'd like to have a list of coordinates here *)

It's possible to convert the graph into a graphics object using Show and extract the coordinates from there. Is there a less hacky, more direct/robust way?

$\endgroup$

3 Answers 3

30
$\begingroup$

In version 8, you can use:

VertexCoordinates /. AbsoluteOptions[g, VertexCoordinates]

AbsoluteOptions is usually a good bet when other things just return Automatic

In version 9, there's the GraphEmbedding function:

GraphEmbedding[g]
$\endgroup$
0
9
$\begingroup$

Perhaps:

Table[PropertyValue[{g, n}, VertexCoordinates], 
  {n, 1, VertexCount[g]}]

(* 
{{1.93552, 0.76408}, {2.51085, 1.17051}, {1.48194, 1.6304}, {1.90242, 
 1.64263}, {0.92823, 1.47388}, {2.31252, 0.126716}, {0., 
 1.08036}, {1.42818, 0.}, {0.554302, 0.210118}, {1.37128, 0.758598}}
*)
$\endgroup$
1
  • $\begingroup$ Is there some way to get all values at once using what one would expect PropertyValue[{g, _}, VertexCoordinates] to do? $\endgroup$
    – ssch
    Nov 14, 2012 at 20:04
0
$\begingroup$

From version 11.3 on, there is also ResourceFunction["VertexCoordinateList"][g]

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.