# How can I have for loop show me each histogram it created rather then play it like a movie?

8 views (last 30 days)
Brooke Chase on 13 Sep 2022
Commented: Star Strider on 14 Sep 2022
I am trying to see each histogram (with a pdf fit to it) for each value within the for loop but when i run it it is "playing" each histogram plot back to back like a short movie. I want to see each histogram and potentailly place it all in one figure if that makes sense. Here is the current code I have for the section.
"
for i=1:10;
idx_age = find(age >= (i-1)*1000 & age < i*1000);
age_tmp = age(idx_age);
iso_tmp = d18O (idx_age);
x1 = iso_tmp
% fit normal distribution to sample data
pd1 = fitdist(x1,'Normal')
mu1 = mean(pd)
sigma1 = std(pd)
% Overlay a plot of the fitted pdf.
x1_pdf = [-50:0.1:-20]
y1 = pdf(pd,x1_pdf)
% then use iso_tmp to make a pdf within for loop and save to a matrix to plot
figure (3)
histogram(x1,'Normalization','pdf')
line(x1_pdf,y1)
"

Star Strider on 13 Sep 2022
I am not certain what you are doing or the result you want. I don’t see an end for the for loop, so I assume everything is in the same loop.
Perhaps a subplot array ( or tiledlayout) would do what you want —
figure(3)
for i=1:10;
idx_age = find(age >= (i-1)*1000 & age < i*1000);
age_tmp = age(idx_age);
iso_tmp = d18O (idx_age);
x1 = iso_tmp
% fit normal distribution to sample data
pd1 = fitdist(x1,'Normal')
mu1 = mean(pd)
sigma1 = std(pd)
% Overlay a plot of the fitted pdf.
x1_pdf = [-50:0.1:-20]
y1 = pdf(pd,x1_pdf)
% then use iso_tmp to make a pdf within for loop and save to a matrix to plot
subplot(5,2,i)
histogram(x1,'Normalization','pdf')
line(x1_pdf,y1)
title(sprintf('Histogram %2d',i))
end
.
Star Strider on 14 Sep 2022
No worries! (I was off doing other things for a while.)
Puitting the pdf’s together without the histograms would suggest a waterfall or ribbon plot.
Plotting histograms and pdf’s together in one axes would likely not be possible without hopelessly confusing all of them. For that reason,.I would put the 10 plots together in one figure either using subplot (similar to what I posted earlier) or tiledlayout. Considering that they all share the same independent variable, stackedplot might also be an option. With all of these, I would use the same axis call (as I outlined earlier) to make differences between them readily apparent, instead of using automatic axis scaling that would obscure the differences.
Use saveas or similar functions to save the figure as an image.

Paul on 13 Sep 2022
What happens if you put the command
hold on
after the figure(3) line?
Brooke Chase on 13 Sep 2022
Okay that helped a little because it color coded each plot in the loop rather than the end result being the last value in the loop. I guess I want a better way to visualize the change because right now I am just seeing a mush of all the plots on top of one another? So maybe a different plot for each value or something?