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

조회 수: 16 (최근 30일)
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
Star Strider 2022년 9월 13일
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
.
  댓글 수: 14
Brooke Chase
Brooke Chase 2022년 9월 14일
One more question.. so sorry. How would you suggest I plot all of the histograms together on one plot? - create a plot together with a color bar. Trying to show a change through time but in one figure (for a proposal)
Star Strider
Star Strider 2022년 9월 14일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Paul
Paul 2022년 9월 13일
What happens if you put the command
hold on
after the figure(3) line?
  댓글 수: 1
Brooke Chase
Brooke Chase 2022년 9월 13일
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?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by