I have an adjacency matrix "am" and I want to color the nodes with four colors. Here I what do...

G = AdjacencyGraph[am];
colors = ColorData[97] /@ Range[4];
Graph[G, GraphStyle -> "BasicBlack", VertexSize -> 1, 
 VertexLabels -> Placed["Name", Center], VertexLabelStyle -> 13, 
 EdgeStyle -> Darker@ColorData["Legacy", "IndianRed"], 
 VertexStyle -> 
  Thread[Range[VertexCount[G]] -> 
    colors[[IGKVertexColoring[G, 4][[1]]]]], 
 ImageSize -> {{600, 900}, {600, 900}}]

Note that with this approach, the vertex labels are are taken automatically from 1 to to the number equal to the number of nodes.

But I want to label the nodes with different numbers from a list.

Lets say ID is a list of numbers. and the labels should be taken from this list.

How can I do it?


1 Answer 1



ids = RandomChoice[CharacterRange["a", "z"], VertexCount[G]];

use the option

VertexLabels -> {v_ :> Placed[Style[ids[[v]], White], Center]

in Graph[...].


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.