19
$\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$
26
$\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$
8
$\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$
  • $\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 '12 at 20:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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