Create a patch which includes all the histogram profiles
5 views (last 30 days)
Show older comments
Hi, I have several profiles/skylines of histograms:
hold on
for i = 1 : 10
X = normrnd(3000,10,[1,1000 ]);
histogram(X, 100 , 'DisplayStyle','stairs');
end
hold off
and I would like to create a patch which includes all the histogram profiles in this way (possibly, including the "highest" and the "lowest" lines/profiles, here in red):
Any idea?
Accepted Answer
AndresVar
on 10 Mar 2022
If you can use uniform bin edges for all series then it can be simplified
Here it is plotting white over the another color. You can improve the fill method to draw a single polygon (instead of 2), so it's truly the patch area. (there many similar question about "shading between two curves")
see:
clear;
numSeries = 10; % number of histograms
X = normrnd(3000,10,[numSeries,1000]); % the data
% specify the bin edges (same for all series)
binLimits = [min(X(:)) max(X(:))];
binWdith = 0.8;
numBins = ceil(diff(binLimits)/binWdith);
% get counts and binEdges
counts = zeros([numSeries,numBins]);
for ii = 1 : size(X,1)
[counts(ii,:),binEdges]=histcounts(X(ii,:), 'BinLimits',binLimits,'BinWidth',binWdith);
end
% get top and bottom
counts_top = max(counts);
counts_bottom = min(counts);
% plot with 'BinCounts'
figure;
hold on;
histogram(BinEdges=binEdges,BinCounts=counts_top,FaceColor='m',LineStyle='none',FaceAlpha=0.3);
histogram(BinEdges=binEdges,BinCounts=counts_bottom,FaceColor='w',FaceAlpha=1,LineStyle='none');
% original histograms
for ii = 1:numSeries
histogram(X(ii,:),binEdges=binEdges,DisplayStyle="stairs");
end
hold off
title('using histogram')
% can also use fill
figure;
x=sort([binEdges(1:end-1) binEdges(2:end)]);
x=[x x(end) x(1)]; % close the polygon
y=[repelem(counts_top,2) 0 0]; % close with y=0
fill(x,y,'r');
hold on;
y=[repelem(counts_bottom,2) 0 0];
fill(x,y,'w');
title('using fill')
More Answers (1)
See Also
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!