Mathematica – specifying 3D vertex coordinates for a 3D graph

This is specific to Mathematica 8.*, where Graphs are atomic expressions

There was no simple way to assign the 3D vertex-coordinates to the vertices of a graph.
Whereas a 2D graph’s vertex-coordinates can be initialized as follows :
[text]
vertices = {1, 2, 3};
coords = {{0, 0}, {1, 0}, {2, 0}};
connectivity = {1 -> 2, 2 -> 3};
myGraph = Graph[connectivity, VertexCoordinates -> coords]
[/text]
the same when tried on a 3D vertex fails :
[text]
vertices = {1, 2, 3};
coords = {{0, 0, 0}, {1, 0, 0}, {2, 0, 0}};
connectivity = {1 -> 2, 2 -> 3};
myGraph = Graph[connectivity, VertexCoordinates -> coords]
[/text]
The workaround was simple – you use a separate property for for each vertex. In my case I used a property called “VertexCoordinate3D” as follows and got the rest of the code up and running :
[text]
vertices = {1, 2, 3};
coords = {{0, 0, 0}, {1, 0, 0}, {2, 0, 0}};
connectivity = {1 -> 2, 2 -> 3};
(*myGraph = Graph[connectivity, VertexCoordinates->coords]*)

myGraph = Graph[connectivity];
(PropertyValue[{myGraph, vertices[[#]]}, “VertexCoordinates3D”] =
coords[[#]]) & /@ Range@Length@VertexList[myGraph];
[/text]

Leave a comment

Your email address will not be published. Required fields are marked *