Plotting multiple histograms in one figure

284 ビュー (過去 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 Center および File ExchangeData Distribution Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by