Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers 2

up vote 14 down vote accepted

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]
share|improve this answer
add comment

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}}
*)
share|improve this answer
    
Is there some way to get all values at once using what one would expect PropertyValue[{g, _}, VertexCoordinates] to do? –  ssch Nov 14 '12 at 20:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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