2
$\begingroup$

I am rendering a family of graphs via GraphPlot3D[AdjacencyMatrix] and I would also like to label the edges. I already use a vertex rendering function to color the vertices.

There are two ways to do this that I'm aware of:

  • EdgeLabels -> [Array]

  • EdgeRenderingFunction -> {Function}

Is there a simpler way to label the edges, analogous to the "manual" method:

GraphPlot3D[edge -> "label"]
$\endgroup$
1
  • $\begingroup$ Could you introduce your code? $\endgroup$
    – LCarvalho
    Commented Dec 20, 2016 at 13:03

1 Answer 1

1
$\begingroup$

Adjacency matrix:

am = {{0, 1, 1, 1}, {0, 0, 1, 1}, {0, 0, 0, 1}, {0, 0, 0, 0}}
sa = SparseArray @ am;

Label matrix:

lm = RandomChoice[DictionaryLookup[x__ /; StringLength[x] == 3][[ ;; 100]], {4, 4}];

Construct edges annotated with labels using the matrices am and lm:

elist = Property[Rule @@ #, EdgeLabels -> lm[[## & @@ #]]] & /@ sa["NonzeroPositions"];

Use Graph3D

g1 = Graph3D[elist, EdgeLabelStyle -> 20, ImageSize -> 500]

enter image description here

If you wish you can use the vertex coordinates used by GraphPlot3D:

SetProperty[g1, VertexCoordinates -> GraphPlot3D[am][[1, 1, 1]]]

enter image description here

Alternatively, you can use GraphPlot3D with a custom EdgeRenderingFunction:

erf = {RGBColor[.5, 0, 0], Line[#], Text[lm[[## & @@ #2]], (#1[[1]] + #1[[2]])/2, 
     Background -> White]} &;

GraphPlot3D[am, EdgeRenderingFunction -> erf]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.