Fixed node locations using graphplot() with Markov chains

6 views (last 30 days)
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,
tm = full(sparse(stagesVec(1:end-1),stagesVec(2:end),1));
stateNames = ["N1" "N2" "N3" "R" "U" "W" ];
mc = dtmc(tm,"StateNames",stateNames);

Accepted Answer

Steven Lord
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);
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);
Christopher McCausland
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!

Sign in to comment.

More Answers (0)


Find more on Strategy & Logic in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by