How can I plot a multilayer graph (2 layer) starting from adjacency matrices?
17 views (last 30 days)
Show older comments
Roberto
on 7 Apr 2023
Answered: Christine Tobler
on 11 Apr 2023
I need to plot a multilayer graph starting from adjacency matrices, like the one shown in the figure.
I have 3 adjacency matrices:
- A_gas (7x7 double): graph with nodes in red;
- A_power (24x24 double): graph with nodes in blue;
- A_interlayer (7x24 double): represents the connections between nodes in red and those in blue.
1 Comment
Ive J
on 7 Apr 2023
maybe something like this?
s1 = [ 3 3 1 4 10 8 4 5 6 8];
t1 = [6 10 10 10 11 11 8 8 11 9];
s2 = [1 1 1 1 1 2 2 7 7 9];
t2 = [2 3 4 5 7 6 7 5 9 6 ];
G1 = digraph(s1, t1);
G2 = digraph(s2, t2);
hold on
ax1 = plot(G1);
ax2 = plot(G2);
Accepted Answer
Christine Tobler
on 11 Apr 2023
Here's an example of how to do this (using just some random data, since I don't have the matrices you mention above).
% Choose some random data for the adjacency matrices
A_gas = rand(7, 7) < 0.05;
A_interlayer = rand(7, 24) < 0.05;
A_power = rand(24, 24) < 0.05;
% Give names to each node, so that they are unique for both blue and red
% nodes
namesBlue = (1:24)+" blue";
namesRed = [11 12 14 16 19 20 24]+" red";
% Combine the adjacency matrices to apply to a larger graph containing all
% nodes
A = [A_power, zeros(24, 7); A_interlayer, A_gas];
g = digraph(A, [namesBlue, namesRed]);
% Plot and recolor all red nodes
p = plot(g);
highlight(p, 25:31, NodeColor='red')
p.NodeLabel = string([1:24 11 12 14 16 19 20 24]);
You could also make this a 3D plot by setting the ZData property of variable p so that all red nodes have a different height than the blue nodes do.
0 Comments
More Answers (1)
Vanshika Vaishnav
on 11 Apr 2023
You can represent the graph with this adjacency matrix:
0 1 2
1 0 3
2 3 0
To construct the graph in MATLAB, input:
A = [0 1 2; 1 0 3; 2 3 0];
node_names = {'A','B','C'};
G = graph(A,node_names)
G =
graph with properties:
Edges: [3×2 table]
Nodes: [3×1 table]
You can plot the directed graph as described in the following documentation in "Creating Graphs">>"Adjacency Matrix".
Also, you can code this way:
s1 = [ 3 3 1 4 10 8 4 5 6 8];
t1 = [6 10 10 10 11 11 8 8 11 9];
s2 = [1 1 1 1 1 2 2 7 7 9];
t2 = [2 3 4 5 7 6 7 5 9 6 ];
G1 = digraph(s1, t1);
G2 = digraph(s2, t2);
hold on
ax1 = plot(G1);
ax2 = plot(G2);
for more information refer this below documentation:
0 Comments
See Also
Categories
Find more on Graph and Network Algorithms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!