2
$\begingroup$

I followed Vertex labels of graph: {index,weight} to display both vertex names and weights.

This works fine on the following code, but fails for the adaptation below it (where I try to place the results in rectangles for vertices.

Correct code:

temp = Graph[{x2 \[DirectedEdge] x1, x2 \[DirectedEdge] x4, 
   x5 \[DirectedEdge] x1, x5 \[DirectedEdge] x4, 
   x3 \[DirectedEdge] x2}, VertexShapeFunction -> "Square"]
temp = SetProperty[temp, 
  VertexCoordinates -> 
   ReflectionTransform[{0, -1}]@GraphEmbedding[temp]]
temp = SetProperty[temp, VertexWeight -> {1, 2, 3, 4, 5}]
temp = SetProperty[
  temp, {VertexLabels -> {v_ :> 
      Placed[{"Name", 
        AnnotationValue[{temp, v}, VertexWeight]}, {Above, Below}]}, 
   ImagePadding -> 20}]

Code which fails (names are just displayed as the word Name.)

relations = {x2 -> x6, x2 -> x7, x5 -> x6, x5 -> x7, x3 -> x2}

    labeling[relations_, weights_] := 
 Graph[relations, 
  VertexCoordinates -> GraphEmbedding[Graph[relations]], 
  EdgeStyle -> Arrowheads[.04], 
  VertexLabels -> Placed["Name", Center], VertexSize -> 0.2, 
  VertexStyle -> White, VertexLabelStyle -> Directive[Red, 15], 
  EdgeStyle -> Black, VertexWeight -> weights, 
  VertexShapeFunction -> "Square"]

g = labeling[relations, {1, 2, 3, 4, 5}]

g = SetProperty[g, {VertexLabels -> Table[i -> 
 Placed[{i, PropertyValue[{g, i}, VertexWeight]}, {Above, 
   Below}], {i, 5}]}]

The following alternative fails too:

g = SetProperty[g, {VertexLabels -> {v_ :> Placed[{"Name", AnnotationValue[{g, v}, VertexWeight]}, {Above, 
    Below}]}}]

Both display a square to enter a vertex, with above it Name and below it a number (the correct weight). Instead of name, it should display the true labels, which are of the form x1 x2 x3 x4 or x5. I want to adjust matters to display both in the box via Center, but first need to figure out why the above code fails. It seems to follow the same syntax (?)

$\endgroup$
5
  • $\begingroup$ MMA version 12.1 The first "incorrect" code seems to work, but not the second $\endgroup$ – Daniel Huber Feb 27 at 13:41
  • $\begingroup$ @DanielHuber I run 12.1. Bit confused at your answer. The first code listed, is listed as "correct". The second as "incorrect". Which is what I experience on 12.1. Do you mean there is a bug? $\endgroup$ – Mike Feb 27 at 16:38
  • 1
    $\begingroup$ By first "incorrect" code I mean the second code. It gives labels "X2","X3"...However, the third code gives labels: "Name" and the number below. $\endgroup$ – Daniel Huber Feb 27 at 17:31
  • $\begingroup$ Does the first "incorrect" code give you labels x2 etc and also weights? If not, it is still not correct since I need both. $\endgroup$ – Mike Feb 27 at 18:17
  • 1
    $\begingroup$ No, it only gives the name. $\endgroup$ – Daniel Huber Feb 27 at 19:09
1
$\begingroup$

1. Replace 5 in the table iterator with VertexList[g]:

g = labeling[relations, {1, 2, 3, 4, 5}];

g = SetProperty[g, {VertexLabels ->
   Table[i -> Placed[{i, PropertyValue[{g, i}, VertexWeight]}, {Above,  Below}],
     {i, VertexList[g]}]}]

enter image description here

Using

g = labeling[relations, {1, 2, 3, 4, 5}];
g = SetProperty[g, {VertexLabels -> 
   Table[Placed[{i, PropertyValue[{g, i}, VertexWeight]}, {Above, Below}], 
     {i, VertexList[g]}]}] 

gives the same picture.

2. Replace "Name" with v in the last code block:

g = labeling[relations, {1, 2, 3, 4, 5}];

g = SetProperty[g, 
 {VertexLabels -> {v_ :> Placed[{v, AnnotationValue[{g, v}, VertexWeight]}, 
    {Above, Below}]}}]

enter image description here

You can also use

g = labeling[relations, {1, 2, 3, 4, 5}];

AnnotationValue[g, VertexLabels] = {v_ :> 
  Placed[{v, AnnotationValue[{g, v}, VertexWeight]}, {Above, Below}]};

g 

to get the same picture.

$\endgroup$
5
  • $\begingroup$ Thanks kglr for the detailed and helpful answer. I understand Name (for vertices) as a default label which Mathematica picks when a graph is specified via its edges. So using x2 -> x6 means that x2 and x6 will be used as names for these vertices (default labels). This remains that way until labels are changed by the coder. If this interpretation is correct, then I don't see in your answer point 2 why v needs to be used rather than "Name". "Name worked fine in the first block of code I gave. Am I missing something? $\endgroup$ – Mike Feb 28 at 10:08
  • $\begingroup$ I have another piece of code which worked fine with "Name", until I added VertexLabelStyle -> Directive[Red, 15] after which it failed. Then I replaced "Name" with v and it worked. Is this because ay change to VertexLabels (as in specifying style) means the labels are no longer "default labels" (Names) and hence the use of Name no longer makes sense? Or is this a bug? And if "Name" fails at times, should one always use v instead (so why use Name in the first place?)? I hope to get some clarity on this as it is tripping me up. $\endgroup$ – Mike Feb 28 at 10:33
  • 1
    $\begingroup$ @Mike, it seems like a bug. A simpler example: Graph[{1 -> 2, 2 -> 3}, VertexLabels -> Placed[{"Name", "Index"}, {Before, After}]] vs Graph[{1 -> 2, 2 -> 3}, VertexLabelStyle -> Directive[Red, 16], VertexLabels -> Placed[{"Name", "Index"}, {Before, After}]] $\endgroup$ – kglr Feb 28 at 11:16
  • $\begingroup$ yes indeed. Should I pass it on to support@wolfram.com ? Or do you prefer to do it? Your example is clear. @kglr $\endgroup$ – Mike Feb 28 at 11:26
  • 1
    $\begingroup$ @Mike, please go ahead with the bug report. $\endgroup$ – kglr Feb 28 at 11:33

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.