6
$\begingroup$

EDIT: I adjusted the vertices to have labels that are integers (like the weights). Can the answer be adapted to this case?

I use simultaneous display of vertices and weights (this topic is related to another question posted here)

I display vertices via the following code (only included for completeness)

vf[{xc_, yc_}, name_, {w_, h_}] := 
  Block[{xmin = xc - w, xmax = xc + w, ymin = yc - h, ymax = yc + h}, 
   Polygon[{{xmin, ymin}, {xmax, ymax}, {xmin, ymax}, {xmax, ymin}}]];

The graph edges are specified as:

relations = {2 -> 6, 2 -> 7, 5 -> 6, 5 -> 7, 3 -> 2}

The following code has a part that colors the vertex labels red

labeling[relations_, weights_] := 
 Graph[relations, 
  VertexCoordinates -> 
   ReflectionTransform[{0, -1}]@GraphEmbedding[Graph[relations]], 
  VertexLabelStyle -> Directive[Red, 15],
  VertexWeight -> weights, VertexShapeFunction -> vf]

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

The vertices xi and their weights are simultaneously displayed by:

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

How can I display the weights in blue, but keep the vertices xi in red?

VertexLabelStyle allows me to set the colour red for vertex labels (displaying both vertices xi and their weigths red in the graph).

There is no VertexLabelStyle to set colours of weights (I believe).

I need to manipulate the weights and vertices separately in the graph, i.e. sometimes move the vertices (xi) which are to remain red and sometimes move the weights (integers) which need to remain blue.

$\endgroup$

2 Answers 2

3
$\begingroup$

You can wrap each label with Style:

g = labeling[relations, {1, 2, 3, 4, 5}];
g1 = SetProperty[g, 
      VertexLabels -> Table[
        i -> Placed[{Style[i, Blue], 
                 Style[PropertyValue[{g, i}, VertexWeight], Red]}, 
               {Before, After}],
       {i, VertexList[g]}]]

enter image description here

Alternatively, you can use patterns instead of Table[...] as follows:

g2 = SetProperty[g, 
  VertexLabels -> {v_ :> 
     Placed[{Style[v, Blue], 
       Style[PropertyValue[{g, v}, VertexWeight], Red]}, 
     {Before, After}]}]

enter image description here

$\endgroup$
5
  • $\begingroup$ When I try to highlight vertex indices red the code fails. Is there a way to fix this. Grappling with these basics still. graph1 = SetProperty[graph1, VertexLabels -> Table[i -> Placed[{Style[i, Blue], Style[PropertyValue[{graph1, i}, VertexIndex], Red]}, {Before, After}], {i, VertexList[graph1]}]] $\endgroup$
    – Michel
    Mar 15, 2021 at 14:47
  • 1
    $\begingroup$ @Mike, use PropertyValue[{graph1, i}, VertexIndex] does not work because VertexIndex is not a property. Use Style[VertexIndex[graph1, i], Red] instead. (Note: properties that works with PropertyValue are in the list PropertyList[graph1]). $\endgroup$
    – kglr
    Mar 15, 2021 at 21:02
  • $\begingroup$ is there a systematic reference that treats these distinctions? (Aside from piecing it together from mathematica's online help)? $\endgroup$
    – Michel
    Mar 16, 2021 at 20:28
  • 1
    $\begingroup$ @Mike, not any that I know of. $\endgroup$
    – kglr
    Mar 16, 2021 at 20:34
  • $\begingroup$ ok, thanks, I've been writing my own for a while, so will keep at it. $\endgroup$
    – Michel
    Mar 17, 2021 at 21:14
3
$\begingroup$

Try e.g.:

g = SetProperty[
  g, {VertexLabels -> 
    Table[i -> 
      Placed[{i, PropertyValue[{g, i}, VertexWeight]}, {Before, 
        After}, If[
         NumericQ[#[[1]]], # /. 
          RGBColor[__] -> RGBColor[0, 0, 1], #] &], {i, 
      VertexList[g]}]}]

enter image description here

$\endgroup$
6
  • $\begingroup$ This answer works. It does not seem a straightforward solution (for this user's level), and I wonder if there is a more direct way? $\endgroup$
    – Michel
    Mar 13, 2021 at 11:52
  • $\begingroup$ Is it possible to use this answer when instead of x1,...,xn, the weights have labels 1 to n? @DanielHuber $\endgroup$
    – Michel
    Mar 13, 2021 at 14:26
  • $\begingroup$ You coud say: c = 0; g = SetProperty[ g, {VertexLabels -> Table[i -> Placed[{i, PropertyValue[{g, i}, VertexWeight]}, {Before, After}, If[ NumericQ[#[[1]]], # /. RGBColor[__] -> If[EvenQ[c++], RGBColor[1, 0, 0], RGBColor[0, 0, 1]], #] &], {i, VertexList[g]}]}] $\endgroup$ Mar 13, 2021 at 14:35
  • $\begingroup$ Could you clarify what the EvenQ achieves? Ah, I see (I think). You are alternating matters. If they labels and weights always are integers, is the NumericQ still necessary? $\endgroup$
    – Michel
    Mar 13, 2021 at 15:02
  • 1
    $\begingroup$ You are right, alternating. And the NumericQ is superfluous. $\endgroup$ Mar 13, 2021 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.