Plotting multiple histograms in one figure

975 Ansichten (letzte 30 Tage)
msh
msh am 11 Apr. 2015
Bearbeitet: dpb am 19 Jun. 2023
Hi,
I have some data points, simulated as follows:
for t=1:10000
H1(t)=normrnd(0,0.05);
H2(t)=normrnd(0,0.10);
H3(t)=normrnd(0,0.30)
end
So essentially I generated three different random variables. I would like to do something very obvious.
I want to plot the histogram of each variable in the SAME graph/figure with the respective curve fitting to visualize the difference in the variances for these three random variables.
How I can implement this in Matlab?
Thanks
  1 Kommentar
Star Strider
Star Strider am 11 Apr. 2015
If you have the Statistics Toolbox, use the histfit function.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

dpb
dpb am 11 Apr. 2015
Another's shown the basics of adding to a plot; I'll note there's no need for loops and generating variables like H1, H2, H3 is generally bad practice in Matalab...use the vector facilities of Matlab, it is, after all, called "MATrix LABoratory" for a reason...
nSamp=10000;
mu=0;
sg=[0.05 0.1 0.3];
H=normrnd(mu,repmat(sg,nSamp,1));
hist(H,100), xlim([-1.3 1.3])
  2 Kommentare
Naser Zormati
Naser Zormati am 19 Jun. 2023
Does not work with UI axes! Is there another approach in this case?
dpb
dpb am 19 Jun. 2023
Bearbeitet: dpb am 19 Jun. 2023
The above was from what is now almost the dark ages in MATLAB version changes time frame... :)
For a uifigure, you'll have to add references to the figure and create the axes in it first...
nSamp=10000;
mu=0;
sg=[0.05 0.1 0.3];
H=normrnd(mu,repmat(sg,nSamp,1));
hUIF=uifigure; % create, save handle to uifigure
hAx=axes(hUIF); % create the axes in that figure, not default
hist(hAx,H,100) % plot into that axes, again not default
xlim(hAx,[-1.3 1.3]) % adjust limits of the specific axes
Since then, hist has been deprecated in favor of histogram; you probably should adjust to it although the above did work locally...

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Chad Greene
Chad Greene am 11 Apr. 2015
After plotting the first histogram, you can use hold on to plot more histograms on top. If you're using Matlab 2014b or later, you can use the histogram function with 'facealpha' to set transparency. If you're using an older version of Matlab you can use histf in a similar fashion. I'm using 2012b here, with Stephen Cobeldick's brewermap function here:
map = brewermap(3,'Set1');
figure
histf(H1,-1.3:.01:1.3,'facecolor',map(1,:),'facealpha',.5,'edgecolor','none')
hold on
histf(H2,-1.3:.01:1.3,'facecolor',map(2,:),'facealpha',.5,'edgecolor','none')
histf(H3,-1.3:.01:1.3,'facecolor',map(3,:),'facealpha',.5,'edgecolor','none')
box off
axis tight
legalpha('H1','H2','H3','location','northwest')
legend boxoff
  4 Kommentare
Ninad Thakoor
Ninad Thakoor am 2 Nov. 2017
In the histf.m add following two lines after line 112 to fix alpha issues.
h.FaceAlpha = FaceAlpha;
h.EdgeAlpha = EdgeAlpha;
siddharth rawat
siddharth rawat am 14 Jan. 2018
Thanks.

Melden Sie sich an, um zu kommentieren.


Image Analyst
Image Analyst am 11 Apr. 2015
Try something like
h1 = 0.05 * randn(1, 10000);
h2 = 0.10 * randn(1, 10000);
h3 = 0.30 * randn(1, 10000);
[counts1, binCenters1] = hist(h1, 500);
[counts2, binCenters2] = hist(h2, 500);
[counts3, binCenters3] = hist(h3, 500);
plot(binCenters1, counts1, 'r-');
hold on;
plot(binCenters2, counts2, 'g-');
plot(binCenters3, counts3, 'b-');
grid on;
% Put up legend.
legend1 = sprintf('mu = %.3f', mean(h1));
legend2 = sprintf('mu = %.3f', mean(h2));
legend3 = sprintf('mu = %.3f', mean(h3));
legend({legend1, legend2, legend3});

Community Treasure Hunt

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

Start Hunting!

Translated by