Normalization of colorbar showing bincounts of binscatter

18 vues (au cours des 30 derniers jours)
Sumera Yamin
Sumera Yamin le 9 Août 2019
Modifié(e) : Adam Danz le 12 Août 2019
I am using bin scatter to plot my 2D data. I want to normalize my bin counts from 0-1 in the color bar instaed of absolute number. I guess its a basic proble, but i am stuck. Any help will be highly appreciated. the example code is shown below. Thank you very much.
x=rand(1,10000);
y=rand(1,10000);
binscatter(x,y,[100,100])
colormap(gca,'jet')

Réponse acceptée

Adam Danz
Adam Danz le 9 Août 2019
Modifié(e) : Adam Danz le 12 Août 2019
I would use histcounts2() to determine the density within each bin. Then you can normalize those data and send it into histogram2(). See comments within the code below for details.
x=rand(1,10000);
y=rand(1,10000);
% compute bin counts
[binCounts, xbin, ybin] = histcounts2(x,y,[100,100]);
% Normalize bin counts to 0:1
binCountsNorm = (binCounts - min(binCounts(:))) ./ range(binCounts(:));
% Plot the results *
histogram2('XBinEdges',xbin,'YBinEdges',ybin,'BinCounts',binCountsNorm, ...
'DisplayStyle','tile','ShowEmptyBins','on') % or you may what "off"
% Add color bar and make sure the color ranges from 0:1
colorbar()
caxis([0,1])
*The histogram2() options require r2016b or later:
  3 commentaires
Adam Danz
Adam Danz le 12 Août 2019
Modifié(e) : Adam Danz le 12 Août 2019
Great! In the image you shared, empty bins appear as white. In my version, they appear as dark blue (which is 0 on the colorbar). If you'd rather them appear as white set 'ShowEmptyBins', to 'off'.
Sumera Yamin
Sumera Yamin le 12 Août 2019
Thanks again

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Distribution Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by