This question is similar but not identical to a related question called: coloring-vertices-using-graphplot . I need to color the vertices of a graph according to a function, f that is defined on the nodes where it takes integer values. Here's a poor mans version of what I would like. Which I morphed from "coloring-vertices-using-graphplot".

f = {1, 1, 2, 2, 20}
aa = Flatten@Position[f, 1]
bb = Flatten@Position[f, 2]
cc = Flatten@Position[f, 20]
vs = Join@@MapThread[Thread[#1 -> #2] &, {{aa, bb, cc}, {Blue, Orange, Red}}]
mm = RandomChoice[{0, 1}, {5, 5}];
GraphPlot3D[mm, VertexRenderingFunction -> ({#2 /. vs, Sphere[#1, 0.05]} &)]

SOmething alone these lines would work since my "node function" f only takes on integer values, say from 1,2, ..., 20 but it seems that it would be better to use a color map.

colorRules = Thread[DeleteDuplicates[f] -> {Blue, Red, Orange}];
mm = RandomChoice[{0, 1}, {5, 5}];

GraphPlot3D[mm, VertexRenderingFunction -> ({f[[#2]] /. colorRules, Sphere[#1, 0.05]} &)]


cF[1] = Blue;
cF[2] = Red;
cF[20] = Orange;
GraphPlot3D[mm, VertexRenderingFunction -> ({cF[f[[#2]]], Sphere[#1, 0.05]} &)]


enter image description here

Alternatively, you can Rescale f and use it as input to ColorData functions:

f2 = {1, 1, 5, 5, 12}; 
GraphPlot3D[mm, VertexRenderingFunction -> ({ColorData["Rainbow"][Rescale[f2][[#2]]],
      Sphere[#1, 0.05]} &)]

enter image description here

  • $\begingroup$ Very cool. I think this is exactly what I was after. Actually I want to use a fixed scale. I'll have a bunch of different versions of f2, one for each time point in a dynamical simulation. Ahh. I see. I can feed "Rescale" a fixed scale and I'm good to go. $\endgroup$
    – JEP
    Dec 12 '14 at 17:55

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.