itit123
itit123

Reputation: 39

change height of vertice treeplot matlab

i want vertex 5 to be in the same height(y=0.5) as vertices 4 6 8 and that vertices 10 and 11 in the same height(y=0.32) as vertices 8 9 and still leave vertices 9 and 10 without children i hope the image is clear put vertex 5 in height y=0.5 https://i.sstatic.net/kBpRH.png 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(12)=6;p(13)=6;
 p(18)=12;p(18)=13;
 p(19)=18;
 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);
 line([x(2) x(6)],[y(2) y(6)],'color','r');

 line([x(2) x(3)],[y(2) y(3)],'color','r');
 X_Midpoint = (x(2) + x(3))/2;
 Y_Midpoint = (y(2) + y(3))/2;
 text(X_Midpoint,Y_Midpoint,["num of";"sharings"],'FontSize',7);
 line([x(6) x(7)],[y(6) y(7)],'color','r');
 X_Midpoint = (x(6) + x(7))/2;
 Y_Midpoint = (y(6) + y(7))/2;
 text(X_Midpoint,Y_Midpoint,["num of";"sharings"],'FontSize',7);


 for i=1:length(x)
  text(x(i),y(i),num2str(i));
 end
 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,'num of sharings')
end

end

Upvotes: 0

Views: 31

Answers (1)

FangQ
FangQ

Reputation: 1544

try this

treeplot(p);

% first, get the handles of the lines and the markers
obj=findobj(gca,'type','line');
xx=get(obj,'xdata');
yy=get(obj,'ydata');

% these are the marker (circles), same as xx{2}/yy{2}
[x,y] = treelayout(p);

% move the marker position for vertex 5
y2=y;
y2(5)=0.5;
set(obj(2),'ydata',y2)

% find all edges that are connected to vertex 5, update y position
idx=find((xx{1}==x(5)) & (yy{1}==y(5)))
y3=yy{1};
y3(idx)=0.5;
set(obj(1),'ydata',y3)

see output below

enter image description here

Upvotes: 1

Related Questions