Plotting multiple histograms in one figure

621 次查看(过去 30 天)
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

采纳的回答

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;

请先登录,再进行评论。


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