Create a patch which includes all the histogram profiles
조회 수: 6 (최근 30일)
이전 댓글 표시
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?
채택된 답변
AndresVar
2022년 3월 10일
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')
추가 답변 (1개)
참고 항목
카테고리
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!