Two histogram in one plot

108 ビュー (過去 30 日間)
Elzbieta
Elzbieta 2024 年 2 月 16 日
コメント済み: Voss 2024 年 2 月 23 日
Hello All,
I am trying to draw multiple histogram in one plot using also if condition:
if(ind==1)
fig=figure;
hold on
h1=histogram(f1,'BinWidth',0.01);
xlim([1 1.4])
hold on
h2=histogram(f2,'BinWidth',0.01);
if(ind==1) % HFD.
fig=figure;
hold on
h1=histogram(f1,'BinWidth',0.01);
xlim([1 1.4])
%title('HFD(without artifacts) with treshold=', strtrsh)
%saveas(fig1,strcat(strcat('HFD(without artifacts) with treshold=', strtrsh),".fig"));
%hold on
%fig2=figure;
h2=histogram(f2,'BinWidth',0.01);
%xlim([1 1.4])
title('HFD with treshold=', strtrsh)
saveas(fig,strcat(strcat('HFD with treshold=', strtrsh), ".fig"));
elseif(ind==2) % mean.
fig=figure;
h1=histogram(f1,'BinWidth',0.025);
xlim([0 1])
hold on
%fig2=figure;
h2=histogram(f2,'BinWidth',0.025);
xlim([0 1])
title('mean with treshold=', strtrsh)
saveas(fig,strcat(strcat('mean with treshold=', strtrsh),".fig"));
elseif(ind==2)
fig=figure;
h1=histogram(f1,'BinWidth',0.025);
xlim([0 1])
hold on
h2=histogram(f2,'BinWidth',0.025);
xlim([0 1])
saveas(fig,strcat(strcat('With treshold=', strtrsh),".fig"));
saveas(fig,strcat(strcat('mean with treshold=', strtrsh),".fig"));
Unfortunately I receive for one case two different figures. From which one is saved with save command and the second one is only displayed. How to correct it to receive only one figure with two histogram? The same one should be displayed and saved?
Cheers
Elzbieta

採用された回答

Voss
Voss 2024 年 2 月 16 日
Here's some code that plots two histograms in one figure and saves the figure:
% some random data:
f1 = 1.2+randn(1,100)/20;
f2 = 1.2+randn(1,100)/20;
% create the figure and histograms:
fig=figure;
hold on
h1=histogram(f1,'BinWidth',0.01);
xlim([1 1.4])
h2=histogram(f2,'BinWidth',0.01);
% save the figure:
saveas(fig,'histograms.fig')
You can see that the saved figure is accurate:
openfig('histograms.fig');
It's difficult to offer more specific advice because your code is a little confusing for the following reasons:
  • The ind==2 branches will never be executed since they are inside the top-level if(ind==1) block - and ind doesn't change. And anyway, the second ind==2 branch will never be executed at all - it doesn't make sense to use the same condition more than once in an if/elseif construction.
  • The code inside the first if(ind==1) and the second if(ind==1) create nearly identical figures, with the only difference being the second figure's plot has a title. I'm not sure what that's about
Be aware that each time you do
fig=figure;
a new figure is created, and the variable fig is the new figure. If you need to refer to a previously created figure after having created a new one, then you should store the two figures as different variable names (e.g., fig1 and fig2), and if you don't mean to create a new figure then remove the fig=figure; line, which will make the subsequent plotting commands plot into the current figure.
  4 件のコメント
Elzbieta
Elzbieta 2024 年 2 月 23 日
移動済み: Voss 2024 年 2 月 23 日
Thank you to all of you.
Voss
Voss 2024 年 2 月 23 日
You're welcome

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by