0
$\begingroup$

I've used TreeForm, and I appreciate that the syntax is fairly short, especially in comparison with TreeGraph.

Is it possible specify a different colors in TreePlot for coloring a section of the nodes and the paths to those nodes?

$\endgroup$

3 Answers 3

4
$\begingroup$
framedWithColor[color_] := Function[{position, label},
  {Text[Framed[label, Background -> color], position]}
];

TreeForm[
  {{1, 2}, {3, 4}},
  VertexRenderingFunction -> framedWithColor[Pink]
]

enter image description here

$\endgroup$
10
$\begingroup$

Anyway, Treegraph offers a lot of flexibility:

nodes = {RandomInteger[#] , # + 1} & /@ Range[0, 30];
rn = Range@Length@nodes;
crules = Rule @@@ Partition[Riffle[rn, ColorData[15, "ColorList"]], 2];
g = TreeGraph[UndirectedEdge @@@ nodes, VertexSize -> 0.4,  VertexStyle -> crules];
HighlightGraph[g, PathGraph@FindShortestPath[g, 1, 30], GraphHighlightStyle -> "Dashed"]

Mathematica graphics

Terse code is nice, flexibility is wonderful.

$\endgroup$
7
  • $\begingroup$ it has one major drawback that u cannot have children of a node who is children of another node $\endgroup$
    – TraceKira
    Commented Nov 27, 2014 at 14:35
  • $\begingroup$ i put a link in my original comment $\endgroup$
    – TraceKira
    Commented Nov 27, 2014 at 14:37
  • $\begingroup$ TreeGraph[] supports all possible operations on Graphs- $\endgroup$ Commented Nov 27, 2014 at 14:37
  • 1
    $\begingroup$ @timoftebogdan TreeGraph[] is much more flexible than TreePlot[] and TreeForm[] for this kind of things. I told you before, but you don't like it :). See my comment under your previous question $\endgroup$ Commented Nov 27, 2014 at 14:41
  • 4
    $\begingroup$ @timoftebogdan Oh, I see. You're confounding the Vertex name with the Vertex Label ! $\endgroup$ Commented Nov 27, 2014 at 14:55
0
$\begingroup$

The terminal nodes of TreeForm are apparently all wrapped in an insidious HoldForm such that one cannot easily give them separate colors.

That is:

ii = 0; 
TreeForm[Nest[{#, ++ii} &, ii, 3],
  VertexRenderingFunction -> ({Black, 
     Text[#2, #1, 
      Background -> (Hue[
         Switch[#2, 0, 0.1, HoldForm@1, 0.2, 2, 0.3, 3, 0.4, List, 
          0.8, _, 0]])]} &)]

treeform bug

$\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.