Node lablel overlaping Edge lable

Node lablels are overlaping Edge lables , as per the attached photo , what can i do to avoide that situation , also i wanna change the Node lable to be on the center of the Node not on the right of it

4 Comments

Provide a small sample case that reproduces the issue...
that case happen when i plot a wide graph (50 Nodes or more ) , but when i only plot a small graph (5 node), there is no over lapping , here is an example of a 4 Nodes graph
attach your codes and data.
networksheet=Minia24NewMinia3DPS1;%This XL sheet is Imported Manually
distrbutionsheet=Minia24NewMinia3DP;%This XL sheet is Imported Manually
NodesTestTable=distrbutionsheet.NameOfRMUNetworkNode;
Edgestesttable=[networksheet.From networksheet.To];
realedges=table(Edgestesttable,'VariableNames',{'EndNodes'});
g=graph(realedges);
startxdata=-1.5;
n=0;
networksheetROWNUMBERS=size(networksheet,1);
distrbutionsheetROWNUMBERS=size(distrbutionsheet,1);
nodessize=size(g.Nodes);
reallength=table(networksheet.Lengthm,'VariableNames',{'Length'});
getedgelable=[realedges reallength];
edgelabletableinM=table(ones(size(networksheet.Lengthm)));
nameoflable=string(table2array(g.Nodes));
poweroflable=string(table2array(g.Nodes));
connecteddp=string(table2array(g.Nodes));
mergedtable=cell(distrbutionsheetROWNUMBERS,1);
%Creating Edge Lable
for idx=1:networksheetROWNUMBERS
subbasin_no=g.Edges{idx,"EndNodes"};
cond1=networksheet.From==subbasin_no{1,1}&networksheet.To==subbasin_no{1,2};
cond2=networksheet.To==subbasin_no{1,1}&networksheet.From==subbasin_no{1,2};
k=find(cond1|cond2,1,"first");
edgelabletableinM.Var1(idx)=networksheet.Lengthm(k);
end
edgelabletableinKM=table2array(edgelabletableinM).*0.001;
%Creating Node Lable
for ndx=1:nodessize
nodcom=g.Nodes{ndx,"Name"};
FLAGID=strcmp(nodcom,networksheet.OutgoingFeeder);
trueflagd=nonzeros(FLAGID);
if trueflagd==1
nameoflable(ndx)=nodcom;
poweroflable(ndx)='';
connecteddp(ndx)='';
else
kk=find(distrbutionsheet.NameOfRMUNetworkNode==g.Nodes{ndx,"Name"});
nameoflable(ndx,1)="_ "+distrbutionsheet.NameOfRMUNetworkNode(kk);
poweroflable(ndx,1)=distrbutionsheet.RatedPowerkVA(kk)+"KVA";
connecteddp(ndx,1)=distrbutionsheet.ConnectedDPName(kk);
end
end
strNodname=string(nameoflable);
strNodpower=string(poweroflable);
strNodeDP=string(connecteddp);
nodeslabletable=[strNodname,strNodpower,strNodeDP];
strnodeslabletable=join(nodeslabletable,newline,2);
%Ploting Graph
h=plot(g,"EdgeLabel",edgelabletableinKM,"NodeLabel",strnodeslabletable,NodeLabelMode="manual",NodeFontName='ArialMT' ...
,NodeFontAngle='normal',NodeFontWeight='normal');
%Adjusting Xdate and YDate to show Feeders sepratly
for id=1:nodessize
nodcom=g.Nodes{id,"Name"};
FLAGID=strcmp(nodcom,networksheet.OutgoingFeeder);
trueflagd=nonzeros(FLAGID);
if trueflagd==1
n=n+1;
startxdata=startxdata+500;
h.YData(:,id)=15;
h.XData(:,id)=startxdata;
else
h.YData(:,id)=h.YData(:,id-1)-1;
h.XData(:,id)=startxdata;
end
end
%Highlieght NO Point of the Network
for i=1:networksheetROWNUMBERS
if networksheet.OutgoingFeeder(i)==networksheet.From(i)
highlight(h,[networksheet.From(i)],"NodeLabelColor","red")
end
if networksheet.NOAssociatedTofromOrtoOrboth(i)=="from"
highlight(h,[networksheet.From(i)],"NodeLabelColor","green")
targetcable=[networksheet.From(i) networksheet.To(i)];
realtragted=table(targetcable,'VariableNames',{'EndNodes'});
gg=graph(realtragted);
highlight(h,gg.Edges{:,"EndNodes"},"EdgeColor","green","LineStyle","--")
elseif networksheet.NOAssociatedTofromOrtoOrboth(i)=="to"
highlight(h,[networksheet.To(i)],"NodeLabelColor","green")
targetcable=[networksheet.From(i) networksheet.To(i)];
realtragted=table(targetcable,'VariableNames',{'EndNodes'});
gg=graph(realtragted);
highlight(h,gg.Edges{:,"EndNodes"},"EdgeColor","green","LineStyle","--")
end
end
%Adjusing Node postion on Figure by darging and release
set(gcf,'WindowButtonDownFcn',@(f,~)edit_graph(f,h))
function edit_graph(f,h)
% Figure out which node is closest to the mouse.
a = ancestor(h,'axes');
pt = a.CurrentPoint(1,1:2);
dx = h.XData - pt(1);
dy = h.YData - pt(2);
len = sqrt(dx.^2 + dy.^2);
[lmin,idx] = min(len);
% If we're too far from a node, just return
tol = max(diff(a.XLim),diff(a.YLim))/20;
if lmin > tol || isempty(idx)
return
end
node = idx(1);
% Install new callbacks on figure
f.WindowButtonMotionFcn = @motion_fcn;
f.WindowButtonUpFcn = @release_fcn;
% A ButtonMotionFcn that changes XData & YData
function motion_fcn(~,~)
newx = a.CurrentPoint(1,1);
newy = a.CurrentPoint(1,2);
h.XData(node) = newx;
h.YData(node) = newy;
drawnow;
end
% A ButtonUpFcn which stops dragging
function release_fcn(~,~)
f.WindowButtonMotionFcn = [];
f.WindowButtonUpFcn = [];
end
end

Sign in to comment.

Answers (1)

Hi Mohamed,
I understand that while plotting the graph, the edge labels are overlapping, and you would like to align the node labels to the centre instead of right alignment.
The overlapping error occurs due to the Adjusting Xdata and YData to show Feeders separately” step where you assign the same Xdata to multiple nodes.
h.XData(:,id)= startxdata;
Instead, you could add a small random number to Xdata to make all the edges visible and remove the overlapping.
h.XData(:,id)= startxdata + 100*rand(1);
In case of aligning the nodes to the centre, there is a no documented property of ‘GraphPlot(https://www.mathworks.com/support/search.html/answers/477770-is-it-possible-to-change-the-position-of-graph-plot-node-labels.html) that allows you to change the placement of the labels relative to the nodes. However, you could remove the labels and replace them with your own matching text labels. That way you have complete control over the placement of each label. The bellow mentioned snippet does this for you:
text(h.XData, h.YData, h.NodeLabel,...
'HorizontalAlignment', 'center', 'FontSize', 8);
h.NodeLabel={};
I am also attaching the updated code for better understanding.
Hope this helps and clarifies your issue regarding overlapping node labels and aligning them to the centre!

Categories

Find more on Mathematics in Help Center and File Exchange

Products

Release

R2021b

Asked:

on 4 Sep 2022

Answered:

on 14 Sep 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!