Create a patch which includes all the histogram profiles
5 ビュー (過去 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!