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
1 个评论
采纳的回答
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 个评论
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
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
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
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});
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!