2
$\begingroup$

The problem I flagged still occurs after replacing Vertexweights by VertexWeight.

The weights are displayed on the graph by $Failed for the following code, but work fine for the code produced after (the only difference is that I used

temp = SetProperty

rather than

SetProperty 

I don't see why this should be the case (I flagged a part of the code that showed up highlighted in blue at the end of this post).

Code which fails

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

Code which works

temp = Graph[{x2 \[DirectedEdge] x1, x2 \[DirectedEdge] x4, 
   x5 \[DirectedEdge] x1, x5 \[DirectedEdge] x4, 
   x3 \[DirectedEdge] x2}]
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}]

The only part that is highlighted in blue when I write the code is the use of x1 (and the others: x2 x3 x4 x5) in the first line of the code

Graph[{x2 \[DirectedEdge] x1, x2 \[DirectedEdge] x4, 
   x5 \[DirectedEdge] x1, x5 \[DirectedEdge] x4, 
   x3 \[DirectedEdge] x2}]
$\endgroup$
11
  • 1
    $\begingroup$ replace VertexWeights with VertexWeight? $\endgroup$
    – kglr
    Feb 26 at 9:05
  • $\begingroup$ That worked. Oddly, VertexWeights did not produce an error. $\endgroup$
    – Mike
    Feb 26 at 9:10
  • 1
    $\begingroup$ SetProperty (and Annotate) does not produce errors because it is supposed to work to add custom properties (which may have arbitrary names). So the only clue that VertexWeights may be problematic is the blue syntax highlighting. $\endgroup$
    – kglr
    Feb 26 at 9:23
  • 1
    $\begingroup$ SetProperty "produce(s) an object with a modified property value." It does not do modification in place. So you need to use temp = SetProperty[temp,...] (as you in the last code block) to make the properties stick to temp. $\endgroup$
    – kglr
    Feb 26 at 10:41
  • 1
    $\begingroup$ Mike, posted the comment as answer. $\endgroup$
    – kglr
    Feb 26 at 11:17
2
$\begingroup$

From SetProperty >> Details

enter image description here

That is, SetProperty[obj,...] does not modify properties in place. We need to do obj = SetProperty[obj, ...] to modify the properties of obj.

Illustration using a simple example:

g0 = Graph[{1 -> 2, 2 -> 3}];
PropertyValue[g0, EdgeStyle]
Automatic
SetProperty[g0, EdgeStyle -> {(1 -> 2) -> Red, (2 -> 3) -> Green}]

enter image description here

PropertyValue[g0, EdgeStyle]
Automatic
g0 = SetProperty[g0, EdgeStyle -> {(1 -> 2) -> Red, (2 -> 3) -> Green}];

PropertyValue[g0, EdgeStyle] 

enter image description here

$\endgroup$

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.