Two histogram in one plot

99 views (last 30 days)
Elzbieta
Elzbieta on 16 Feb 2024
Commented: Voss on 23 Feb 2024
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

Accepted Answer

Voss
Voss on 16 Feb 2024
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 Comments
Elzbieta
Elzbieta on 23 Feb 2024
Moved: Voss on 23 Feb 2024
Thank you to all of you.
Voss
Voss on 23 Feb 2024
You're welcome

Sign in to comment.

More Answers (0)

Categories

Find more on Data Distribution Plots 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!