# Fixed node locations using graphplot() with Markov chains

1 view (last 30 days)
Christopher McCausland on 18 Feb 2022
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 on 18 Feb 2022
Store the handle of the GraphPlot in a variable, h1 in the code below.
adjacency1 = sprand(6, 6, 0.2);
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); ##### 2 CommentsShowHide 1 older comment
Christopher McCausland on 21 Feb 2022
Hi Steven,
Never mind I have discovered my error, you (very cleverly) use the digraph() command inside the plot() command, for anyone else, it should look like this:
nodePlace = graphplot(mc);
D2 = digraph(mc.P,mc.StateNames); % Different digraph
h2 = plot(D2, 'XData', nodePlace.XData, 'YData', nodePlace.YData);
I will probably generate two vectors at the start for the X and Y data but this now makes much more sense in terms of the the calls are handled. Thank you so much for your help!
Christopher

R2021b

### Community Treasure Hunt

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

Start Hunting!