# Extracting points and lines from Graph

I was wondering if there is an easy way to extract lines and/or points from a Graph e.g.

myGraph=Graph[{1 \[UndirectedEdge] 2, 2 \[UndirectedEdge] 3, 3 \[UndirectedEdge] 1}]


One way to extract the lines and points (together with their styles) from the above graph would be to "Copy as" the output of the graph in the form of a Notebook Expression and then paste it without interpreting the context of the text (there is a dialogue box appearing, select "No"). Then with some more lines of code we could extract the desired information e.g. from the GraphicsComplexBox, EdgeForm etc.

Note that you can add axes to your graph, in order to get a rough idea about the coordinates, by adding Axes->True i.e.

myGraph=Graph[{1 \[UndirectedEdge] 2, 2 \[UndirectedEdge] 3, 3 \[UndirectedEdge] 1},Axes->True]


Is there any other more straightforward way to extract the lines and/or points from a Graph?

Thanks,

Dimitris

• Perhaps AbsoluteOptions@myGraph is useful, as in AbsoluteOptions[myGraph,#]&/@{VertexStyle,VertexCoordinates,EdgeStyle}.
Apr 3 at 21:56
• Can you clarify what you mean by "lines" and "points"? Do you mean edges and vertices? Do you mean vertex coordinates? Apr 4 at 15:34
• @Szabolcs, good point! I was thinking from a Plot perspective, points: (nodes or vertices) and lines: (edges or links)
– demm
Apr 4 at 21:44

To get VertexCoordinates you can use GraphEmbedding or PropertyValue:

GraphEmbedding[myGraph]

{{-0.866025, -0.5}, {0.866025, -0.5}, {1.83697*10^-16, 1.}}

PropertyValue[{myGraph, #}, VertexCoordinates] & /@ VertexList[myGraph]

{{-0.866025, -0.5}, {0.866025, -0.5}, {1.83697*10^-16, 1.}}


To extract the graphics primitives you can use Show[myGraph] to get a Graphics object and extract desired primitives using Cases. For example,

arrows = Cases[Normal @ Show @ myGraph, _Arrow, All]


disks = Cases[Normal @ Show @ myGraph, _Disk, All]