Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add node numbers/get node locations from MATLAB's treeplot

Tags:

plot

tree

matlab

I have been working with MATLAB's treeplot function, but it seems to provide surprisingly little plotting functionality and/or extendibility.

I am plotting a tree like so:

tree = [0  1  2  2  2  2  2  1  8  8  1  11  11  1  14];
treeplot(tree)

Giving: enter image description here

What I would like to do is add annotations or labels to specific nodes. A good starter would be to add the node numbers to each node, as in the example from the help file:

enter image description here

As they state, though:

These indices are shown only for the point of illustrating the example; they are not part of the treeplot output.

Is there a way to get the locations of the plotted nodes, or at the very least to plot the node numbers? I couldn't find any FEX submissions with more advanced tree plots.

Ultimately, I'd like to plot small pictures at the nodes (using methods from answers to a previous question of mine).

like image 837
Bill Cheatham Avatar asked Dec 16 '22 17:12

Bill Cheatham


2 Answers

This should help you make a labeled tree: (You supply the 'treeVec'.)

treeplot(treeVec);
count = size(treeVec,2);
[x,y] = treelayout(treeVec);
x = x';
y = y';
name1 = cellstr(num2str((1:count)'));
text(x(:,1), y(:,1), name1, 'VerticalAlignment','bottom','HorizontalAlignment','right')
title({'Level Lines'},'FontSize',12,'FontName','Times New Roman');

With your sample input, this gives enter image description here

like image 55
HelpAStranger Avatar answered Dec 29 '22 00:12

HelpAStranger


To get the position of the nodes, use treelayout

[x,y]=treelayout(tree);

The vectors x and y give you the positions, which you can then use to plot images at the nodes.

like image 32
Ghaul Avatar answered Dec 28 '22 22:12

Ghaul