Paul
Paul

Reputation: 6911

MATLAB's treeplot: align by node height from top

Here's what I get by using the treeplot function on MATLAB (this is the example image):

enter image description here

Here's what I'd like to get:

enter image description here

As you can see, I'd like to have the position of each node according to its distance from the root. Is that possible?

Upvotes: 1

Views: 502

Answers (1)

cferner
cferner

Reputation: 25

I was also looking for a "root-aligned" treeplot in Matlab and found no solution. Here is what I came up with, in case someone is still in need of it (I'm using the same example as in the Matlab documentation):

nodes = [0 1 2 2 4 4 4 1 8 8 10 10];

At first we need to get the x and y coordinates of every node in the original tree plot and find all leaves in it:

[x,y] = treelayout(nodes);
leaves = find( y == min(y) );

Next, we reconstruct every chain in the tree plot and store it in a matrix (by doing so, we can later change the y position of the nodes):

num_layers = 1/min(y)-1;
chains = zeros(num_layers, length(leaves));
for l=1:length(leaves)
    index = leaves(l);
    chain = [];
    chain(1) = index;
    parent_index = nodes(index);
    j = 2;
    while (parent_index ~= 0)
        chain(j) = parent_index;
        parent_index = nodes(parent_index);
        j = j+1;
    end
    chains(:,l) = padarray(flip(chain), [0, num_layers-length(chain)], 'post');
end

Now we compute the new y-coordinates determined by the row index in the matrix and dependent on the number of layers in the tree:

y_new = zeros(size(y));
for i=1:length(nodes)
    [r,c] = find(chains==i, 1);
    y_new(i) = max(y) - (r-1)*1/(num_layers+1);
end

We can now plot the re-positioned nodes and add the connecting lines:

plot(x, y_new, 'o');
hold on
for c=1:size(chains, 2)
    line_x = x(chains(chains(:,c)>0, c));
    line_y = y_new(chains(chains(:,c)>0, c));
    line(line_x, line_y);
end

If you like, you can also add the node labels to the plot:

for t=1:length(nodes)
    text(x(t)+0.025, y_new(t), num2str(t));
end
xlim([0 1]);
ylim([0 1]);

The resulting figure looks as follows: root-aligned treeplot

Upvotes: 1

Related Questions