5
$\begingroup$

I would like to label vertices of a graph with an ordered pair of the vertex Name (or Index), and the VertexWeight. This seems natural:

g = Graph[{1 <-> 2, 2 <-> 3, 3 <-> 1}, VertexWeight -> {102, 103, 104},
  VertexLabels -> "Name"];
Graph[g,
 VertexLabels -> 
  Table[i -> {i, PropertyValue[{g, i}, VertexWeight]}, {i, 3}]]

But it overprints the vertex name and the vertex weight:


enter image description here
If I use instead i/PropertyValue[{g, i}, VertexWeight] I get a nice display as fractions $\frac{1}{102}$ etc., but if there is cancellation this fails.

The documentation says

Any expression can be used as a label.

so I should be able to accomplish this...

$\endgroup$
2
  • $\begingroup$ Use something like Row[{i, "/", PropertyValue[{g, i}, VertexWeight]}] instead $\endgroup$
    – Carl Woll
    Aug 25, 2017 at 20:52
  • $\begingroup$ @CarlWoll: Brilliant! Thanks so much! $\endgroup$ Aug 25, 2017 at 20:54

1 Answer 1

3
$\begingroup$
SetProperty[g, {VertexLabels -> 
   Table[i -> Placed[{i, PropertyValue[{g, i}, VertexWeight]}, {Above, Below}], {i, 3}], 
  ImagePadding -> 20}]

or, slightly shorter,

 SetProperty[g, {VertexLabels -> 
  {v_:> Placed[{"Name", PropertyValue[{g, v}, VertexWeight]}, {Above, Below}]},
  ImagePadding -> 20}]

both give

enter image description here

$\endgroup$
2
  • $\begingroup$ Very nice! Thank you! $\endgroup$ Aug 25, 2017 at 21:04
  • $\begingroup$ @JosephO'Rourke, my pleasure. $\endgroup$
    – kglr
    Aug 25, 2017 at 21:04

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.