4
$\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$
  • $\begingroup$ Use something like Row[{i, "/", PropertyValue[{g, i}, VertexWeight]}] instead $\endgroup$ – Carl Woll Aug 25 '17 at 20:52
  • $\begingroup$ @CarlWoll: Brilliant! Thanks so much! $\endgroup$ – Joseph O'Rourke Aug 25 '17 at 20:54
2
$\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$
  • $\begingroup$ Very nice! Thank you! $\endgroup$ – Joseph O'Rourke Aug 25 '17 at 21:04
  • $\begingroup$ @JosephO'Rourke, my pleasure. $\endgroup$ – kglr Aug 25 '17 at 21:04

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.