I have some list of numbers which I am plotting using TreePlot.

This is the code:

Plotting[S_, u_, d_, n_] := 
  Module[{coor = Flatten[Table[Table[{i, j - 1 - i/2}, {j, 1, i + 1}], {i, 0, n}],
         steps = Flatten[Table[ Table[
         {S*u^k*d^(l - k) -> S*u^k*d^(l - k + 1), S*u^k*d^(l - k) -> S*u^(k + 1)*d^(l - k)}, 
                         {k, 0, l}], {l, 0, n - 1}]]}, 
   TreePlot[steps, DirectedEdges -> True, VertexLabeling -> True, 
            VertexCoordinateRules -> coor, PlotLabel -> S]];

Plotting[S, u, d, 4]

And now, i need some of the nodes to be painted in different color. For example dSu, Su^3 and d^4S.

Is there a way to do it?


3 Answers 3


One easy way is to replace the style of the specific nodes in the final tree. Let's make a function for it:

colorize[tree_, nodes_List] := With[{patt = Alternatives @@ nodes},
  tree /. Framed[p : patt, style_, r2___] :> 
    Framed[p, style /. c_RGBColor :> Darker[c], r2]

Now you can do

t = Plotting[S, u, d, 4];
colorize[t, {S, d^2 S*u^2, d^3 S}]

Mathematica graphics

Some explanation

To see how a single node is represented in the final graphics, you could look at the InputForm of the final tree. There you see, that each node is a Text element:

Cases[t, Text[__], 5] // InputForm

(* ...
Text[Framed[S, {Background -> RGBColor[1, 1, 0.8], 
  FrameStyle -> RGBColor[0.94, 0.85, 0.36], 
  FrameMargins -> Automatic}], 1]

Now you can go and replace all style settings you want. For a short example, I did nothing more than darken all found colors, but you are of course free to change it to whatever you like.

  • $\begingroup$ Wow, thanks a lot, just what i needed :) Cheers $\endgroup$
    – Trinica
    Commented Apr 18, 2014 at 20:53

You could set up a dynamic VertexRenderingFunction that allows you to change the colors of your vertices with a click.

colorClickVRF[colors_List] := Function[{pos, name},
  Module[{i, len},
   i = 1;
   len = Length[colors];
     backColor = Lighter[First[colors]],
     frameColor = Darker[First[colors]]},
    EventHandler[{Text[Framed[Style[name, Black], 
        Background -> Dynamic[backColor],
        FrameStyle -> Dynamic[frameColor]], pos]},
     {"MouseClicked" :> (i = Mod[i + 1, len, 1];
        backColor = Lighter[colors[[i]]];
        frameColor = Darker[colors[[i]]])}]

Note that colorClickVRF accepts a list of colors and returns a function that is acceptable as either a VertexRenderingFunction for GraphPlot or TreePlot as you are using. It also works as a VertexShapeFunction for the newer Graph and friends. Here's an example.

g = RandomGraph[{10, 30}, VertexShapeFunction -> 
  colorClickVRF[{Yellow, Red, Lighter[Blue]}]]

enter image description here

When first generated, all nodes appear yellow. The other colors you see were obtained by clicking on the nodes once or twice. In your case, you simply need to add this same option to your call to TreePlot:

enter image description here

I originally set this up to help me color my NCAA tournament bracket near the bottom of this math.se question. Unfortunately, that was about the best thing that came out of that little project.

tr = Plotting[S, u, d, 4];
vt = {S, d^2 S*u^2, d^3 S};
MapAt[# /. fr:Framed[Alternatives@@ vt, ___]:> MapAt[Darker, fr, {{2, 1, 2}}] &, tr, {1}]


pos = Join @@ (Position[tr, Framed[#, ___]] & /@ vt);
MapAt[Orange &, tr, Join[#, {2, 1, 2}] & /@ pos]


tr2 = tr;
(tr2[[Sequence @@ #]] = Orange) & /@ (Join[#, {2, 1, 2}] & /@ pos);

enter image description here


Your Answer

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

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