function hHist = normalized_histogram2(hAxes,xVals,yVals,xEdges,yEdges)
hist_Counts = histcounts2(xVals,yVals,xEdges,yEdges);
hist_Counts_normalized = (hist_Counts / sum(hist_Counts,'all'))*100;
hHist = histogram2(hAxes,'XBinEdges',xEdges,'YBinEdges',yEdges,'DisplayStyle','tile','BinCounts',hist_Counts_normalized);
colorbar;
xBinCenters = hHist.XBinEdges - [0,diff(hHist.XBinEdges)/2];
yBinCenters = hHist.YBinEdges - [0,diff(hHist.YBinEdges)/2];
for i = 2:numel(xBinCenters)
for j = 2:numel(yBinCenters)
x_loc = xBinCenters(i);
y_loc = yBinCenters(j);
if ~(round(hHist.Values(i-1,j-1),2) == 0)
text(hAxes,x_loc,y_loc,[num2str(round(hHist.Values(i-1,j-1),2)),'%'],'Color',[0 0 0],'FontSize',10,'FontWeight','bold');
else
continue
end
end
end
Best Answer