Message Boards Message Boards

[?] Create Multi-coloured tree plot?

Posted 7 years ago
POSTED BY: Marc Edwards

Marc,

The If comparison needs to be a node, not a number:

nodes = {{0} -> {0, 1}, {0} -> {0, 2}, {0} -> {0, 3}, {0, 1} -> {0, 1,
     4}, {0, 1} -> {0, 1, 5}, {0, 1} -> {0, 1, 6}, {0, 1} -> {0, 1, 
    7}, {0, 1} -> {0, 1, 8}, {0, 1} -> {0, 1}}
TreePlot[nodes, 
 EdgeRenderingFunction -> ({If[First[#2] == {0, 1}, Red, Green], 
     Arrow[#1]} &), 
 VertexRenderingFunction -> (Inset[
     Row[{If[Last[#2] > 3, Rotate[Last[#2], 90 Degree], 
        Last[#2]]}], #1, Background -> White] &)]

to get

enter image description here

or a single node:

TreePlot[nodes,EdgeRenderingFunction->({If[First[#2]=={0},Red,Green],Arrow[#1]}&),VertexRenderingFunction->(Inset[Row[{If[Last[#2]>3,Rotate[Last[#2],90 Degree],Last[#2]]}],#1,Background->White]&)]

to get

enter image description here

I am not sure why testing if Last[#2] equals a number throws an error since Mathematica knows that a list can't equal a number but either way, you need a list in that position, not a number. I guess by throwing an error it lets you know that it can never do that test properly.

POSTED BY: Neil Singer
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract