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 CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by