1
$\begingroup$

Is it possible to have multiple labels appear on one vertex?

Let's say I want to show numbers on vertex always, and when I hover over a vertex, I want label to appear as tooltip. Is that possible?

$\endgroup$
2
$\begingroup$

Update:

tencities = Take[EntityList[EntityClass["City", "UnitedStatesCapitals"]], 10];

names = #["Name"] & /@ tencities
 {"Montgomery", "Juneau", "Phoenix", "Little Rock", "Sacramento",
"Denver", "Hartford", "Dover", "Tallahassee", "Atlanta"}
pops = QuantityMagnitude[#@"Population"] & /@ tencities
 {199518, 32094, 1626078, 198606, 501901, 704621, 123400, 37538, 191049, 486290}
labelingAssoc = AssociationThread[names, 
   Placed[{Last@#, Framed[#2, Background -> White]},
       {Tooltip, Center}] & @@@ Transpose[{tencities, pops}]];

SeedRandom[1]
edges = UndirectedEdge @@@ RandomSample[Subsets[names, {2}], 15];


Graph[names, edges, ImageSize -> Large, VertexLabels -> {v_ :> labelingAssoc[v]}]

enter image description here

Original answer:

edges = {"A" -> "B", "B" -> "C", "C" -> "A"};
vertices = VertexList @ edges;

Graph[edges, VertexSize -> .07, 
 VertexLabels ->  MapIndexed[# -> Placed[Tooltip[#, #2[[1]]], Center] &, vertices]]

enter image description here

Alternatively,

Graph[edges, VertexSize -> .07, 
 VertexLabels -> MapIndexed[# -> Placed[{#, #2[[1]]}, {Center, Tooltip}] &, vertices]]

same picture

Also

g0 = Graph[edges];
SetProperty[g0, {VertexSize -> .07, 
  VertexLabels -> {v_ :> Placed[{v, VertexIndex[g0, v]}, {Center, Tooltip}]}}]

same picture

$\endgroup$
2
  • $\begingroup$ This is close, but I want to put some other numbers (I have a list of them) instead of vertex index. I have a list of cities and each has a different number value "connected" to it, and I want to show numbers instead of A, B, C, and the tooltip should contain name of the city. $\endgroup$
    – me995
    Mar 31 '20 at 11:38
  • 1
    $\begingroup$ @me995, please see the update. $\endgroup$
    – kglr
    Mar 31 '20 at 12:27

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.