# Extracting vertex coordinates from an automatically laid out graph

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?

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]


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}}
*)

• Is there some way to get all values at once using what one would expect PropertyValue[{g, _}, VertexCoordinates] to do?
– ssch
Nov 14, 2012 at 20:04

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