4
$\begingroup$

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

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

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]

enter image description here

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

enter image description here

$\endgroup$

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.