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

1 visualizzazione (ultimi 30 giorni)
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 Commenti
Waseem AL Aqqad
Waseem AL Aqqad il 7 Ott 2020
I guess I figured it out.
node5=[datah(1,5) datag(1,6)];
node6=[datah(2,5),datag(2,6)];
plot(node5,node6);

Accedi per commentare.

Risposte (1)

Luciano Garim
Luciano Garim il 7 Ott 2020
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!
  1 Commento
Waseem AL Aqqad
Waseem AL Aqqad il 7 Ott 2020
Hi Luciano,
Thanks for your reply!
My mistake was not arranging the coordinates correctly

Accedi per commentare.

Categorie

Scopri di più su Graph and Network Algorithms in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by