Many 2D histograms, single colorbar

조회 수: 2 (최근 30일)
z8080
z8080 2021년 9월 8일
댓글: z8080 2021년 9월 8일
Consider this minimal working example:
for i=1:4
numberPairs(i).matrix = randn(1000,2);
subplot (2,2,i)
h(i) = histogram2(numberPairs(i).matrix(:,1), numberPairs(i).matrix(:,2), 'DisplayStyle','tile', 'ShowEmptyBins','on');
end
% add single colorbar heatmap, for all subplots
h_colorbar = colorbar;
h_colorbar.Position = [ 0.9224 0.1093 0.0133 0.8159 ];
As far as I can see, the colorbar gets created subplot-wise, so its range of values reflects only the final matrix. However, the colors get normalized within each matrix, such that the highest count in each matrix is still bright yellow regardless of the value.
Instead, I'd like this heatmap legend to reflect the values in all subplots. Thus, if the highest-count cell in one subplot is 40, that should be a fainter yellow than the brightest cell in another subplot where the value is 50.
Perhaps this needs to be done not by customising the colorbar, but the 2D histograms themselves - but I don't know how...
Thanks in advance for any suggestions.

채택된 답변

Bjorn Gustavsson
Bjorn Gustavsson 2021년 9월 8일
For that type of equalization I do someething like this:
for i1 = 1:4
numberPairs(i1).matrix = randn(1000,2);
subplot (2,2,i1)
h(i1) = histogram2(numberPairs(i1).matrix(:,1), numberPairs(i1).matrix(:,2), 'DisplayStyle','tile', 'ShowEmptyBins','on');
cx(i1,:) = caxis; % Save away the intensity limits
end
for i1 = 1:4
subplot (2,2,i1)
caxis([min(cx(:,1)),max(cx(:,2))]) % From the smallest low to the largest high
end
h_colorbar = colorbar;
h_colorbar.Position = [ 0.9224 0.1093 0.0133 0.8159 ];
HTH
  댓글 수: 1
z8080
z8080 2021년 9월 8일
Indeed, that's what I needed. Thank you Bjorn!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by