# Graphs: colouring vertex weights differently from vertices

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.

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]}]] 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}]}] • 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]}]] Mar 15, 2021 at 14:47
• @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]).
– kglr
Mar 15, 2021 at 21:02
• is there a systematic reference that treats these distinctions? (Aside from piecing it together from mathematica's online help)? Mar 16, 2021 at 20:28
• @Mike, not any that I know of.
– kglr
Mar 16, 2021 at 20:34
• ok, thanks, I've been writing my own for a while, so will keep at it. Mar 17, 2021 at 21:14

Try e.g.:

g = SetProperty[
g, {VertexLabels ->
Table[i ->
Placed[{i, PropertyValue[{g, i}, VertexWeight]}, {Before,
After}, If[
NumericQ[#[]], # /.
RGBColor[__] -> RGBColor[0, 0, 1], #] &], {i,
VertexList[g]}]}] • 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? Mar 13, 2021 at 11:52
• Is it possible to use this answer when instead of x1,...,xn, the weights have labels 1 to n? @DanielHuber Mar 13, 2021 at 14:26
• You coud say: c = 0; g = SetProperty[ g, {VertexLabels -> Table[i -> Placed[{i, PropertyValue[{g, i}, VertexWeight]}, {Before, After}, If[ NumericQ[#[]], # /. RGBColor[__] -> If[EvenQ[c++], RGBColor[1, 0, 0], RGBColor[0, 0, 1]], #] &], {i, VertexList[g]}]}] Mar 13, 2021 at 14:35
• 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? Mar 13, 2021 at 15:02
• You are right, alternating. And the NumericQ is superfluous. Mar 13, 2021 at 15:46