How to use "hold on" for plotmatrix?
26 views (last 30 days)
Show older comments
Carleen McKenna
on 19 Aug 2021
Commented: Carleen McKenna
on 20 Aug 2021
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
Accepted Answer
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:
- 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)
- 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)
- 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)
See Also
Categories
Find more on Bar Plots 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!