Uistack with a Graph and Subgraphs (GraphPlot)

6 ビュー (過去 30 日間)
Sim
Sim 2024 年 6 月 14 日
編集済み: Sim 2024 年 6 月 17 日
Introduction. I have the following Graph and Subgraphs:
% Graph
s = [1 1 1 3 3 6 7 8 9 10 4 12 13 5 15 16 17 18 19 19 20 20 17 24 25 4 27 28 29];
t = [2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30];
G = graph(s,t);
% Node ID: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
G.Nodes.X = [2 1 3 2 4 4 5 4 5 4 5 1 3 3 5 6 7 6 8 7 9 8 9 8 9 10 1 1 0 1]';
G.Nodes.Y = [2 1 3 9 3 5 8 12 13 18 21 15 18 21 0 2 8 12 15 20 10 22 18 5 4 4 5 8 12 23]';
% Subgraphs
Gpath{1} = subgraph(G,shortestpath(G,4,14));
Gpath{2} = subgraph(G,shortestpath(G,4,26));
Gpath{3} = subgraph(G,shortestpath(G,4,30));
Gpath{4} = subgraph(G,shortestpath(G,3,10));
Gpath{5} = subgraph(G,shortestpath(G,3,28));
Gpath{6} = subgraph(G,shortestpath(G,3,21));
Gpath{7} = subgraph(G,shortestpath(G,17,12));
Gpath{8} = subgraph(G,shortestpath(G,17,23));
Gpath{9} = subgraph(G,shortestpath(G,17,26));
% Figure
hold on
p(1) = plot(G,'XData',G.Nodes.X,'YData',G.Nodes.Y,'LineWidth',1,'EdgeColor','k','NodeColor','k');
for i = [1 2 3]
p(2) = plot(Gpath{i},'XData',Gpath{i}.Nodes.X,'YData',Gpath{i}.Nodes.Y,'EdgeColor','y','NodeColor','y');
p(2).NodeLabel = {};
p(2).EdgeAlpha = 1;
p(2).LineWidth = 5;
end
for i = [7 8 9]
p(3) = plot(Gpath{i},'XData',Gpath{i}.Nodes.X,'YData',Gpath{i}.Nodes.Y,'EdgeColor','g','NodeColor','g');
p(3).NodeLabel = {};
p(3).EdgeAlpha = 1;
p(3).LineWidth = 9;
p(3).DisplayName = 'Apple';
end
for i = [4 5 6]
p(4) = plot(Gpath{i},'XData',Gpath{i}.Nodes.X,'YData',Gpath{i}.Nodes.Y,'EdgeColor','r','NodeColor','r');
p(4).NodeLabel = {};
p(4).EdgeAlpha = 1;
p(4).LineWidth = 15;
p(4).DisplayName = 'Strawberry';
end
legend(p)
Just for clarity, here below I plot the 4 GraphPlots pairwise, i.e. p(1) with p(2), p(1) with p(3), and p(1) with p(4):
Problem. When I try the to use uistack to change the visual stacking of the 4 GraphPlots, by bringing to the bottom both the GraphPlots called "Apple" (in green) and "Strawberry" (in red)...
% Select the GraphPlots which have a non-empty "DisplayName", i.e. "Apple"
% and "Strawberry", and use Uistack
idx = find(~cellfun(@isempty,{p.DisplayName}));
p2 = uistack(p(idx),'bottom')
legend(p2)
...only some single Subgraphs are reshuffled. To the best of my understanding, this means that uistack does not act on the 4 GraphPlots of "p" as a whole, but, instead, it acts on the single 10 GraphPlots of "p2", i.e on the single Subgraphs:
Question. How to get what I expect, i.e. the following plot, where the entire 4 GraphPlots of "p" are reshuffled (and not just some single Subgraphs)?
Note. I have noticed that "p" and the "uistacked p", i.e. "p2", have different dimensions, and I guess it is the source of the problem:
p =
1×4 GraphPlot array:
GraphPlot GraphPlot GraphPlot GraphPlot
p2 =
10×1 GraphPlot array:
GraphPlot
GraphPlot
GraphPlot
GraphPlot
GraphPlot
GraphPlot
GraphPlot
GraphPlot
GraphPlot
GraphPlot
  1 件のコメント
Sim
Sim 2024 年 6 月 14 日
編集済み: Sim 2024 年 6 月 14 日
Hi, any suggestion here? :-)
Is my issue clear enough??

サインインしてコメントする。

回答 (1 件)

Sim
Sim 2024 年 6 月 14 日
編集済み: Sim 2024 年 6 月 17 日
I might have found a solution:
% Graph
s = [1 1 1 3 3 6 7 8 9 10 4 12 13 5 15 16 17 18 19 19 20 20 17 24 25 4 27 28 29];
t = [2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30];
G = graph(s,t);
% Node ID: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
G.Nodes.X = [2 1 3 2 4 4 5 4 5 4 5 1 3 3 5 6 7 6 8 7 9 8 9 8 9 10 1 1 0 1]';
G.Nodes.Y = [2 1 3 9 3 5 8 12 13 18 21 15 18 21 0 2 8 12 15 20 10 22 18 5 4 4 5 8 12 23]';
% Subgraphs
Gpath{1} = subgraph(G,shortestpath(G,4,14));
Gpath{2} = subgraph(G,shortestpath(G,4,26));
Gpath{3} = subgraph(G,shortestpath(G,4,30));
Gpath{4} = subgraph(G,shortestpath(G,3,10));
Gpath{5} = subgraph(G,shortestpath(G,3,28));
Gpath{6} = subgraph(G,shortestpath(G,3,21));
Gpath{7} = subgraph(G,shortestpath(G,17,12));
Gpath{8} = subgraph(G,shortestpath(G,17,23));
Gpath{9} = subgraph(G,shortestpath(G,17,26));
% Figure
hold on
p(1) = plot(G,'XData',G.Nodes.X,'YData',G.Nodes.Y,'LineWidth',1,'EdgeColor','k','NodeColor','k');
for i = [1 2 3]
p(2) = plot(Gpath{i},'XData',Gpath{i}.Nodes.X,'YData',Gpath{i}.Nodes.Y,'EdgeColor','y','NodeColor','y');
p(2).NodeLabel = {};
p(2).EdgeAlpha = 1;
p(2).LineWidth = 5;
end
for i = [7 8 9]
p(3) = plot(Gpath{i},'XData',Gpath{i}.Nodes.X,'YData',Gpath{i}.Nodes.Y,'EdgeColor','g','NodeColor','g');
p(3).NodeLabel = {};
p(3).EdgeAlpha = 1;
p(3).LineWidth = 9;
p(3).DisplayName = 'Apple';
end
for i = [4 5 6]
p(4) = plot(Gpath{i},'XData',Gpath{i}.Nodes.X,'YData',Gpath{i}.Nodes.Y,'EdgeColor','r','NodeColor','r');
p(4).NodeLabel = {};
p(4).EdgeAlpha = 1;
p(4).LineWidth = 15;
p(4).DisplayName = 'Strawberry';
end
text(8,.2,'hello') % <-- I added some text that I need
% Solution
a = get(gca,'Children');
b = findobj('Type','GraphPlot');
idx1 = find(~cellfun(@isempty,{b.DisplayName})); % GraphPlots with name
idx2 = find(cellfun(@isempty,{b.DisplayName})); % GraphPlots without name
c = vertcat(flipud(b(idx2)),flipud(b(idx1)));
set(gca,'Children',[c; setdiff(a,b)])
legend(c)

Community Treasure Hunt

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

Start Hunting!

Translated by