MATLAB Answers

How to use "hold on" for plotmatrix?

23 views (last 30 days)
I am currently trying to run multiple simulations and superimpose all the plots of the sampled probability density functions from the simulation over one another on the same figure via "hold on." For the histogram function this seems to work if I am plotting one variable, but if I am plotting multiple variables to show the marginal and joint distributions I would want to use plotmatrix. However, it looks like plotmatrix and "hold on" are not exactly compatible and don't produce the same result as using "hold on" with histogram.
The format of my code for doing this looks roughly like this (simplified for generality):
n = 1;
while n <= 5
samples = my_function(parameters);
figure(1)
histogram(samples)
hold on
end
What I wanted to do was simply:
plotmatrix(samples)
hold on
Instead of using histogram for when I want to plot more than one variable. But I tried using plotmatrix for just one variable thinking that it would do the same thing as histogram and it doesn't - the plots were not superimposed at all and instead replaced one another.
What would be the proper way to achieve what I want to do?
  2 Comments
Carleen McKenna
Carleen McKenna on 20 Aug 2021
Sorry, I must not have been clear. I was trying to make the question more general. To be less general, I'm running a type of monte carlo sampling process, and I'm running it a few times in a row for the same variable. I don't want the marginal and joint distributions all on one plot. I want it so that it has the format of plot matrix, but all the runs are superimposed together, with the resulting joint and marginal distributions on their respective axes.
I gave histogram as an example of what I wanted because if I'm sampling only one variable, I run the sampling process five times, and use "hold on" so that each resulting distribution displays on the same axis. I want this, but in the form of plotmatrix, where each axis in plotmatrix has five different distributions plotted on it from five different runs.

Sign in to comment.

Accepted Answer

Dave B
Dave B on 20 Aug 2021
Edited: Dave B on 20 Aug 2021
plotmatrix, because it uses multiple axes, doesn't work well at all with hold on...as you discovered.
I can think of three solutions:
  1. The gplotmatrix function in the Statistics and Machine Learning Toolbox is geared towards a plotmatrix with multiple groups. It would require a bit of reorganizing your data (i.e. put all of the sets into one matrix, and label which one is which with a grouping variable). This is probably the easiest approach (if you have the toolbox)
  2. A way to hack around this, which IMO is a little on the ugly side, is to make a second plotmatrix in a new figure and then move the elements to the old figure. A few extra steps here for aligning the limits and getting separate colors. (example with two datasets below)
  3. I think if it were me, I'd be tempted to write my own mini-plotmatrix code that was equipped to handle these data...but that's a bit more involved. Really plotmatrix is just a bunch of scatters and histograms, and it was written long before tiledlayout and nexttile were introduced which makes doing the layout work significantly easier. But writing even a simple chart is an undertaking!
x1=abs(randn(1000,4));
x2=-abs(randn(1000,4));
[a,b,c,d,e]=plotmatrix(x1);
ftemp=figure('Visible','off');
[a2,b2,c2,d2,e2]=plotmatrix(x2);
for i = 1:numel(a2)
a2(i).Parent=b(i);
a2(i).SeriesIndex=2;
% Take union of limits:
xlim(b(i),[min([b(i).XLim(1) b2(i).XLim(1)]) max([b(i).XLim(2) b2(i).XLim(2)])])
ylim(b(i),[min([b(i).YLim(1) b2(i).YLim(1)]) max([b(i).YLim(2) b2(i).YLim(2)])])
end
for i = 1:numel(d2)
d2(i).Parent=e(i);
d2(i).SeriesIndex=2;
xlim(e(i),[min([e(i).XLim(1) e2(i).XLim(1)]) max([e(i).XLim(2) e2(i).XLim(2)])])
ylim(e(i),[min([e(i).YLim(1) e2(i).YLim(1)]) max([e(i).YLim(2) e2(i).YLim(2)])])
end
close(ftemp)

More Answers (0)

Community Treasure Hunt

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

Start Hunting!