How can I have for loop show me each histogram it created rather then play it like a movie?
18 views (last 30 days)
Show older comments
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)
"
0 Comments
Accepted Answer
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.
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
.
14 Comments
Star Strider
on 14 Sep 2022
No worries! (I was off doing other things for a while.)
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.
More Answers (1)
See Also
Categories
Find more on Annotations 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!