Fixed node locations using graphplot() with Markov chains

조회 수: 2 (최근 30일)
Christopher McCausland
Christopher McCausland 2022년 2월 18일
댓글: Steven Lord 2023년 5월 2일
Hi,
I have developed a few lines of code to generate markov chains from a transition vector ('stagesVec'). The code works well and I now wish to compare multiple transition vectors. If I run the code a second time with a second transition vector I get a new Markov chain with the new infromation.
My problem is this, the two Markov chains have the same nodes, however the nodes move (plotting location) between graphs, this makes a visual inspection very difficult. I would like for the nodes to remain in the same place, but edges (the connecting lines between the nodes) to be removed as required by the probability matrix. I will include a couple of images below to hopefully make this easier to understand! I have had a look at both doc graphplot and figure -> edit but cannot see anything obvious.
Thanks for the help,
Christopher
tm = full(sparse(stagesVec(1:end-1),stagesVec(2:end),1));
stateNames = ["N1" "N2" "N3" "R" "U" "W" ];
mc = dtmc(tm,"StateNames",stateNames);
figure()
graphplot(mc);

채택된 답변

Steven Lord
Steven Lord 2022년 2월 18일
Store the handle of the GraphPlot in a variable, h1 in the code below.
adjacency1 = sprand(6, 6, 0.2);
D1 = digraph(adjacency1);
h1 = plot(D1);
When you plot the second graph or digraph, specify the XData and YData name-value pair arguments using the XData and YData properties of the GraphPlot created from the first graph or digraph.
adjacency2 = sprand(6, 6, 0.2);
D2 = digraph(adjacency2); % Different digraph
h2 = plot(D2, 'XData', h1.XData, 'YData', h1.YData);
To show that the second digraph, if left to its own devices, would plot the nodes in different locations:
h3 = plot(D2);
  댓글 수: 8
Zeeshan Mumtaz
Zeeshan Mumtaz 2023년 5월 2일
@Steven Lord Thanks alot. It worked well for me. I have initiated a loop so that all successive nodes align themselves with the previous one.
for i=1:n-1
h.YData(i+1) = h.YData(i);
end
Steven Lord
Steven Lord 2023년 5월 2일
You could use scalar expansion
A=[0.5,0.4,0.07,0.03;0.5,0.4,0.07,0.03;0.5,0.4,0.07,0.03;0.5,0.4,0.07,0.03];
mc=dtmc(A);
mc.StateNames=["E0" "E1" "E2" "E3"];
nodePlace = graphplot(mc);
D2 = digraph(mc.P,mc.StateNames); % Different digraph
h = plot(D2);
layout(h, 'layered', 'Direction', 'right', 'Sources', 'E0', 'Sinks', 'E3');
h.YData(1:3) = h.YData(2); % Leaving E3 in place so you can see that E0 & E2 moved
You could use : instead of 1:3 if you want to move all the nodes to the same Y coordinates as E1.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by