Plotting multiple histograms in one figure

조회 수: 178 (최근 30일)
msh
msh 2015년 4월 11일
편집: dpb 2023년 6월 19일
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
Star Strider
Star Strider 2015년 4월 11일
If you have the Statistics Toolbox, use the histfit function.

댓글을 달려면 로그인하십시오.

채택된 답변

dpb
dpb 2015년 4월 11일
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
Naser Zormati
Naser Zormati 2023년 6월 19일
Does not work with UI axes! Is there another approach in this case?
dpb
dpb 2023년 6월 19일
편집: dpb 2023년 6월 19일
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...

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Chad Greene
Chad Greene 2015년 4월 11일
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
Ninad Thakoor
Ninad Thakoor 2017년 11월 2일
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 2018년 1월 14일
Thanks.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2015년 4월 11일
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});

카테고리

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