fh = figure();
clf()
data = randi(3,8,8)-1;
data(logical(eye(8))) = NaN;
hm = heatmap(data);
colormap([1 0 0; 1 1 0; 0 1 0])
hmp = hm.Position;
cbax = axes('Position',[sum(hmp([1,3])), 0, 1-sum(hmp([1,3])), 1],...
'XTick',[], 'YTick', [], 'Color',fh.Color);
cbax.XAxis.Visible = 'off';
cbax.YAxis.Visible = 'off';
cbax.Colormap = hm.Colormap;
cbh = colorbar(cbax,'Position',[.90, hmp(2), .03, hmp(4)]);
cbh.Limits = [0,1];
nColors = size(hm.Colormap,1);
cbh.Ticks = (1/nColors : 1/nColors : 1) - 1/nColors/2;
cbh.TickLabels = 0:nColors-1;
cbh.FontSize = hm.FontSize;
Best Answer