How can I display a graph with its weights inside the vertices and its labels just outside?

I have the following graph:

Graph[
{1, 2, 3, 4},
{{1, 2}, {1, 3}, {1, 4}} // Map[Apply[UndirectedEdge]],
VertexWeight -> {10, 20, 30, 40},
VertexLabels -> Placed["VertexWeight", Center],
VertexSize -> Medium
]


which is displayed as follows:

But what I would like is to have the weights inside the circles, and the names, i.e., {1, 2, 3, 4}, just outside each circle.

What is the best way to achieve that?

A simple way is to draw the graph twice with different labels:

Show[
Graph[{1, 2, 3, 4},
UndirectedEdge @@@ {{1, 2}, {1, 3}, {1, 4}},
VertexWeight -> {10, 20, 30, 40},
VertexLabels -> #,
VertexSize -> Medium] & /@
{"Name", Placed["VertexWeight", Center]}]


• Thanks! I had tried VertexLabels -> {"Name", Placed["VertexWeight", Center]}, but did not think to render the graph twice. Dec 8 '21 at 18:38

If getting a Graphics object is ok Bob Hanlon's method is the cleanest way I can think of.

If you need a Graph object with multiple vertex labels, you can do it in a number of ways, including:

1.

g = Graph[{1, 2, 3, 4},
UndirectedEdge @@@ {{1, 2}, {1, 3}, {1, 4}} ,
VertexWeight -> {10, 20, 30, 40},
VertexSize -> Medium];

Graph[g, VertexLabels ->
{v_ :> Placed[{AnnotationValue[{g, v}, VertexWeight], v}, {Center, {After, Above}}]}]


2.

(AnnotationValue[{g, #}, VertexLabels] =
Placed[{AnnotationValue[{g, #}, VertexWeight], #}, {Center, {After, Above}}]) & /@
VertexList[g];
g


3.

vWeights = AssociationThread[Range[4], {10, 20, 30, 40}];

Graph[{1, 2, 3, 4},
UndirectedEdge @@@ {{1, 2}, {1, 3}, {1, 4}} ,
VertexWeight -> Values @ vWeights,
VertexLabels -> {v_ :>
Placed[{vWeights @ v, "Name"}, {Center, {After, Above}}]},
VertexSize -> Medium]


• Your first method is very good. Thanks! Dec 9 '21 at 7:35
• @kglr: Is there some way to show the $2$ to be on the RHS of the node like the others?
– Moo
Dec 9 '21 at 14:09
• @Moo, the relative positions (with respect to the vertex positions) of all four labels are the same.
– kglr
Dec 9 '21 at 17:39
• @kglr: I totally missed that, however, visually, it is not very clean looking.
– Moo
Dec 9 '21 at 17:41
• @Moo, perhaps you can try something like Placed[{AnnotationValue[{g, v}, VertexWeight], v}, {Center, {1.25, .75}}].
– kglr
Dec 9 '21 at 17:47