Fixed node locations using graphplot() with Markov chains

12 vues (au cours des 30 derniers jours)
Christopher McCausland
Christopher McCausland le 18 Fév 2022
Commenté : Steven Lord le 2 Mai 2023
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);

Réponse acceptée

Steven Lord
Steven Lord le 18 Fév 2022
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 commentaires
Zeeshan Mumtaz
Zeeshan Mumtaz le 2 Mai 2023
@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 le 2 Mai 2023
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by