Hi @Simon Page
I understand that you are trying to display a "2D binscatter" ("histogram2") such that the values represent the percentage distribution of displacement values within each "x-axis" bin, rather than expressing each bincount as a percentage of the total number of datapoints.
When using MATLAB’s "histogram2" function with "Normalization","probability", the histogram is normalized globally, meaning each bin count is divided by the total number of input datapoints. This is why your current implementation reflects global percentages rather than "per-xbin" percentages.
To resolve this, normalize each "x-bin" independently by computing "histcounts2" manually and applying per-row normalization. Kindly refer to the following corrected implementation:
% Define bin edges
xEdges = min(xVals):60:max(xVals);
yEdges = min(yVals):1:max(yVals);
% Compute raw 2D histogram
hist_Counts = histcounts2(xVals, yVals, xEdges, yEdges);
% Normalize each x-bin independently
hist_Counts_normalized = zeros(size(hist_Counts));
for i = 1:size(hist_Counts,1)
row_sum = sum(hist_Counts(i,:));
if row_sum ~= 0
hist_Counts_normalized(i,:) = hist_Counts(i,:) / row_sum * 100;
end
end
% Plot the normalized histogram
hHist = histogram2(hAxes, ...
'XBinEdges', xEdges, ...
'YBinEdges', yEdges, ...
'BinCounts', hist_Counts_normalized, ...
'DisplayStyle', 'tile');
colorbar;
caxis([0 100]); % Scale colorbar from 0 to 100%
% Add axis limits
xlim([0 2400])
ylim([-6 6])
% Compute bin centers
xBinCenters = hHist.XBinEdges(1:end-1) + diff(hHist.XBinEdges)/2;
yBinCenters = hHist.YBinEdges(1:end-1) + diff(hHist.YBinEdges)/2;
% Annotate bins with values
for i = 1:numel(xBinCenters)
for j = 1:numel(yBinCenters)
val = hist_Counts_normalized(i,j);
if val >= 0.2
text(hAxes, xBinCenters(i), yBinCenters(j), ...
[num2str(round(val,2)),'%'], ...
'Color', [0 0 0], 'FontSize', 10, 'FontWeight', 'bold');
end
end
end
This solution ensures that each time window ("x-bin") reflects the local distribution of displacements ("y-bins"), summing to 100% per column. The "colorbar" and "bin" annotations now accurately show localized behavior over time.
For further reference on "histogram bin normalization", you may refer to the following official documentation:
- "histcounts2" function in MATLAB: https://www.mathworks.com/help/matlab/ref/histcounts2.html
- "histogram2" function in MATLAB: https://www.mathworks.com/help/matlab/ref/matlab.graphics.chart.primitive.histogram2.html
I hope this helps!