Is there a way to control the labels of nodes in a graph - that is, suppressing the names of some nodes but showing the names of other nodes? For example, consider the graph:

edges = {N1 -> N2, N2 -> N3, N3 -> N4, N4 -> N5, N6 -> N2, N6 -> N3, 
  N6 -> N4, N1 -> N8, N2 -> N8, N4 -> N8, N7 -> N8, N9 -> N7, N9 -> N8}
g = Graph[edges, VertexLabels -> Automatic]

Setting VertexLabels->None suppresses the names of all nodes. How can I control this such that ALL nodes in the graph are without labels, except for those whose names I specify to be labeled? For instance, in the above graph suppose I want only nodes N1 and N6 to be labeled but all the other nodes to not be labeled?


1 Answer 1

Graph[edges, VertexLabels -> {N1 | N6 -> "Name"}]


Graph[edges, VertexLabels -> {N1 | N6 -> Automatic}]

enter image description here

To attach arbitrary labels to nodes 1 and 6, say, "label 1" and "label 2", use

 VertexLabels -> Thread[{N1, N6} -> {"label 1", "label 6"}]]

enter image description here

  • $\begingroup$ Wonderful; once again many thanks kglr!! $\endgroup$
    – user42700
    Dec 8, 2021 at 3:56
  • $\begingroup$ @PRG, my pleasure. $\endgroup$
    – kglr
    Dec 8, 2021 at 3:57

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.