7
$\begingroup$

I use the following code to plot a binary tree

TreePlot[{11 -> 23, 11 -> 24, 23 -> 40, 23 -> 39, 24 -> 30, 24 -> 50, 
  40 -> 55, 40 -> 45}, VertexLabeling -> All, 
 PlotStyle -> {FontSize -> 13}, 
 VertexRenderingFunction -> ({EdgeForm[White], 
     RGBColor[113/255, 190/255, 236/255], Disk[#1, 0.2], White, 
     Text[#2, #1]} &), AspectRatio -> Automatic]

enter image description here

The question is what should I do if I want to change the back color of vertex "11" and "23" to red? Thank you!

$\endgroup$
2
  • $\begingroup$ Hi, use TreeGraph with VertexShapeFunction and then SetProperty[{g, 11}, VertexShapeFunction -> ({EdgeForm[White], Red, Disk[#1, 0.2], White, Text[#2, #1]} &)] $\endgroup$
    – Kuba
    Commented Nov 16, 2015 at 12:50
  • $\begingroup$ @Kuba Thanks! Do you know how do I change the font size in TreeGraph? TreeGraph seems not to support PlotStyle directive. $\endgroup$
    – Tony
    Commented Nov 16, 2015 at 13:00

3 Answers 3

5
$\begingroup$
ar = ConstantArray[RGBColor[113/255, 190/255, 236/255], 55];
ar[[{11, 23}]] = Red;



 TreePlot[{11 -> 23, 11 -> 24, 23 -> 40, 23 -> 39, 24 -> 30, 24 -> 50, 
      40 -> 55, 40 -> 45},
     VertexLabeling -> All,
     PlotStyle -> {FontSize -> 13},
     VertexRenderingFunction -> 
        ({EdgeForm[White], ar[[#2]], Disk[#1, 0.2], White, Text[#2, #1]} &),
     AspectRatio -> Automatic]

enter image description here

$\endgroup$
2
$\begingroup$

Here's an alternative approach that does the work in VertexRenderingFunction:

$redDisks = {11, 23};

TreePlot[{11 -> 23, 11 -> 24, 23 -> 40, 23 -> 39, 24 -> 30, 24 -> 50, 
  40 -> 55, 40 -> 45}, VertexLabeling -> All, 
 PlotStyle -> {FontSize -> 13}, VertexRenderingFunction -> 
  ({EdgeForm[White], If[MemberQ[$redDisks, #2], Red, 
     RGBColor[113/255, 190/255, 236/255]], Disk[#1, 0.2], White, 
    Text[#2, #1]} & ), AspectRatio -> Automatic]

enter image description here

$\endgroup$
1
$\begingroup$

You can also post-process the output of TreePlot to change the styles of select vertices:

ClearAll[modify]
modify = # /. d : Disk[(Alternatives @@ Cases[#, Text[Alternatives @@ #2, p_] :> p, 
 {0, Infinity}]), _] :> {#3, d} &;

Examples:

tp = TreePlot[{11 -> 23, 11 -> 24, 23 -> 40, 23 -> 39, 24 -> 30, 
    24 -> 50, 40 -> 55, 40 -> 45}, VertexLabeling -> All, 
   PlotStyle -> {FontSize -> 13}, 
   VertexRenderingFunction -> ({EdgeForm[White], 
       RGBColor[113/255, 190/255, 236/255], Disk[#1, 0.2], White, 
       Text[#2, #1]} &), AspectRatio -> Automatic];

modify[tp, {11, 23}, Red]

enter image description here

Fold[modify[#, #2[[1]], #2[[2]]] &, tp, {{{11, 23}, Red}, {{45, 50}, Purple}}]

enter image description here

$\endgroup$

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.