How to build links between two graphs on the same figure?

1 次查看(过去 30 天)
Hi,
I am encountering a problem trying to simulate interdependent systems (graphs), I constructed and built two different graphs on the same figure, and now I'm trying to add interlinks (lines) between the graphs' different nodes. Could you kindly check my script below?
Thanks!
clear all;
%Edges of first Graph
s=[1 1 2 3 3 5];
t=[2 6 7 4 5 6];
%st=vertcat(s,t);
%Edges of Second Graph
e=[1 2 2 3 4 5 5 6];
r=[3 4 7 8 1 3 2 7];
%er=vertcat(e,r);
%Constructing the Graphs
h=graph(e,r);
g=graph(s,t,[]);
g.Nodes.Name={'1' '2' '3' '4' '5' '6' '7'}';
h.Nodes.Label={'source' 'target' 'Xuewei' 'Hassan' 'TAM' 'Muath' 'Alexander' 'Waseem'}';
%X and Y coordinates of two Graphs
x1 = [0 0.5 -0.5 -0.5 0.5 0 1.5];
y1 = [0 0.5 0.5 -0.5 -0.5 2 5 ];
x2=[1 1.5 0.5 0.5 1.5 2 2.5 3];
y2=[1 1.5 1.5 0 0 3 1.5 2];
%Plotting both graphs on the same figure
w=plot(g,'-dr','XData',x1,'YData',y1);
hold on
y=plot(h,'ko--','XData',x2,'YData',y2);
% labelnode(y,[1 2 3 4 5 6 7 8],{'source' 'target','Waseem' 'Hassan','Xuewei' 'Muath','Waseem' 'Alexander'})
%Concatenation of XData and YData and add an interlink
datah=cat(1,w.XData,w.YData);
% plot(datah(:,1),datah(:,4),'k');
datag=cat(1,y.XData,y.YData);
line(datah(:,5),datag(:,6));
  3 个评论
Waseem AL Aqqad
Waseem AL Aqqad 2020-10-7
I guess I figured it out.
node5=[datah(1,5) datag(1,6)];
node6=[datah(2,5),datag(2,6)];
plot(node5,node6);

请先登录,再进行评论。

回答(1 个)

Luciano Garim
Luciano Garim 2020-10-7
Hi, Waseem AL Aqqad.
If your problem is only how to view these graphs together, you may proceed like this:
Create the G that is all connected. After, use subgraph to produce your g and h graphs.
I hope helped you!

类别

Help CenterFile Exchange 中查找有关 Graph and Network Algorithms 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by