MATLAB: How to color histogram bars based on their height when using HIST3 in MATLAB 7.5 (R2007b)

Statistics and Machine Learning Toolbox

I would like to able to color the bars created by the HIST3 function according to their height.

Best Answer

This change has been incorporated into the documentation in Release 2009b (R2009b). For previous releases, read below for any additional information:
The main idea behind coloring the histogram bars is to first retrieve handles to those bars and then set the relevant properties. Below is an example code that illustrates this idea:
% Create data to be graphed
data = mvnrnd([1 2], [1 1.1; 1.1 2], 1000);
% Create histogram plot
hist3(data,[10 10],'FaceAlpha',.85);
xlabel('X'); ylabel('Y');
% Get handle of surface plotted to change color settings
surfHandle = get(gca, 'child');
% Set the color to change with bar value
set(surfHandle,'FaceColor','interp', 'CdataMode', 'auto');