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

19 ビュー (過去 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 Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by