How can I plot a multilayer graph (2 layer) starting from adjacency matrices?
13 次查看(过去 30 天)
显示 更早的评论
I need to plot a multilayer graph starting from adjacency matrices, like the one shown in the figure.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1348419/image.jpeg)
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 个评论
Ive J
2023-4-7
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);
采纳的回答
Christine Tobler
2023-4-11
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 个评论
更多回答(1 个)
Vanshika Vaishnav
2023-4-11
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graph and Network Algorithms 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!