How can I write on the lines that connects the vertices (edges) treeplot?
MATLAB code:
p(1)=0;
p(2)=1;p(3)=1;
p(6)=3;p(7)=3;
p(5)=2;p(4)=2;
p(10)=5;p(11)=5;
p(8)=4;p(9)=4;
p(16)=8;p(17)=9;
p(14)=7;p(15)=7;
p(20)=14;p(19)=14;
p(20)=15;p(19)=15;
%p.edgelable=0;
treeplot(p);
[x,y] = treelayout(p);
for i=1:length(y)
text(x(i),y(i),strcat('a',num2str(i)))
end
The midpoints between the Parent_Node and the children can be calculated by taking the average of the two corresponding x and y positions. Then using the text() function as you already know. Adding the 'HorizontalAlignment' property to 'center' will also ensure the label's text is justified to the middle of the relative to the mid-point positions.
• X mid-point = (x1 - x2) / 2
• Y mid-point = (y1 - y2) / 2

p(1)=0;
p(2)=1;p(3)=1;
p(6)=3;p(7)=3;
p(5)=2;p(4)=2;
p(10)=5;p(11)=5;
p(8)=4;p(9)=4;
p(16)=8;p(17)=9;
p(14)=7;p(15)=7;
p(20)=14;p(19)=14;
p(20)=15;p(19)=15;
treeplot(p);
[x,y] = treelayout(p);
for i=1:length(y)
text(x(i),y(i),strcat('a',num2str(i)))
end
%Labelling branches%
for i = 2: length(p)
Parent_Node = p(i);
if(Parent_Node > 0)
X_Midpoint = (x(i) + x(Parent_Node))/2;
Y_Midpoint = (y(i) + y(Parent_Node))/2;
text(X_Midpoint,Y_Midpoint,num2str(Parent_Node) + "->" + num2str(i),'HorizontalAlignment','center');
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With