After creating the bivariate histrogram with hist3(), call the function again with the same inputs but this time, store the first output which is the binvariate bin count N = hist3(___). This will not produce a plot which is why it must be called separately from the plot. Assuming the bivariate histrogram was produced with default CDataMapping property, you can scale the bin counts to the axes colormap range to compute the RGB values for each bin.
x = rand(10,2);
hist3(x,'CdataMode','auto')
count = hist3(x);
view(2)
ax = gca();
cm = ax.Colormap;
count = round((count-min(count(:)))./max(count(:)).*(size(cm,1)-1) + 1);
rgbArray = permute(reshape(cm(count,:).', [3,size(count)]),[2,3,1]);
Investigate the count matrix to explore the mapping between the count bins and your histrogram bins. You'll find that the first column of count represents the bottom row of bins in the plot. Therefore rgbArray(i,j,:) contains the color for the i-th column and j-th row from the bottom. You may want to do some transposing or flipping to orient the array to your liking.
hist3() produces a surface object. Check out more surface properties here:
Best Answer