Two histogram in one plot

108 visualizaciones (últimos 30 días)
Elzbieta
Elzbieta el 16 de Feb. de 2024
Comentada: Voss el 23 de Feb. de 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

Respuesta aceptada

Voss
Voss el 16 de Feb. de 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 comentarios
Elzbieta
Elzbieta el 23 de Feb. de 2024
Movida: Voss el 23 de Feb. de 2024
Thank you to all of you.
Voss
Voss el 23 de Feb. de 2024
You're welcome

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Distribution Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by