Finding x position of color-grouped boxchart boxes for overlaying jittered errorbar plot
22 views (last 30 days)
Show older comments
Theo Husby
on 1 Oct 2025 at 2:54
Answered: Theo Husby
on 1 Oct 2025 at 15:23
I'm writing a GUI that creates a boxchart from the results of a fitting operation. Each data point has an x and color category and a confidence interval associated with it. I'd like to create a boxchart that groups data points categorically on x and color, and then overlay an errorbar plot that shows each individual data point data point and its confidence interval. There could be any number of color and x categories.
My issue is that I can't figure out how to calculate or find the x position of each of the boxes, which I need to ensure the errorbar plots line up. It seems like this should be a property of the BoxChart object, but I can't find it.
For example, if I create the following data set, which uses either a two or three color categories:
% Generate some data
xcat = categorical({'a', 'b', 'c'});
ccat = categorical({'c1'; 'c2'; 'c3'}); % 3 color categories
%ccat = categorical({'c1'; 'c2'; 'c2'}); % 2 color categories
xdata = reshape(repmat(xcat, 12, 1), [], 1);
cdata = reshape(repmat(ccat, 4, 3), [], 1);
ydata = reshape([normrnd(10, 1, 12, 1), normrnd(11, 1, 12, 1), normrnd(8, 1, 12, 1)], [], 1);
cfdata = reshape(normrnd(0, 0.5, 12, 3), [], 1);
And I create a boxchart from it like this:
% Convert from categorical to numerical for plot
xcat_names = categories(xdata);
xdata_num = double(xdata);
% Create boxchart
figure
b = boxchart(xdata_num, ydata, 'GroupByColor', cdata);
set(gca, "XTick", unique(xdata_num), "XTickLabel", xcat_names);
legend
The following error bar code works perfectly when there's three colors, but doesn't align properly if there's two:
% Estimate errorbar positions
ccat_names = categories(cdata);
cgroupwidth = 1-1/length(ccat_names);
xspacing = cgroupwidth/length(ccat_names);
xcoloroffset = (0:length(ccat_names)-1) * xspacing - cgroupwidth/2 + xspacing/2;
% Add some jitter for readability
xjitteroffset = zeros(size(xdata)); % Zeros for testing alignment with no jitter
%xjitteroffset = randn(size(xdata))*cgroupwidth/length(ccat_names)/4;
% Overlay jittered errorbars on boxchart
cmap_c = lines(length(categories(cdata))); % match color of boxchart
hold on
for ix = 1:length(xcat_names)
for ic = 1:length(ccat_names)
ibox = xdata == xcat(ix) & cdata == ccat(ic); % find data that fits criteria
errorbar(xdata_num(ibox) + xcoloroffset(ic) + xjitteroffset(ibox),...
ydata(ibox), cfdata(ibox), '.', 'Color', cmap_c(ic,:),...
'HandleVisibility','off')
end
end
grid on
This code results in the following graphs depending on which ccat definition is commented out. You can see that the errorbars don't align in the 2 Color scenario.

So in summary, I'm interested if anyone knows:
- A way to get the x position of the boxes from the BoxChart object
- A way to calculate the x position of the boxes reliably with varying numbers of color and x categories.
2 Comments
Mathieu NOE
on 1 Oct 2025 at 10:14
A similar question was raised here :
A possible solution to your problem might be here :
Accepted Answer
More Answers (0)
See Also
Categories
Find more on Errorbars in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!