Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 18 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

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

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.