Here's a demo you can follow to add error bars to a grouped bar chart.
To center the errorbar on each bar, you need to compute their center points along the x axis. That is done using an undocumented property XOffset explained here. Required inputs are
- count: an n x m matrix that will produce n groups each containing m bars.
- err: an n x m matrix that defines the error for each bar
count = [9862.5 5238 3845.2; 9368.6 3515 3625.7; 11064 6810 4073.6; 12599.2 5701 3955.3; 10114.4 5683 3664.5];
err = (rand(size(count))+2)*100;
figure;
h = bar(count);
xCnt = (get(h(1),'XData') + cell2mat(get(h,'XOffset'))).';
hold on
errorbar(xCnt(:), count(:), err(:), err(:), 'k', 'LineStyle','none')
Alternatively, if you're working with error intervals that define the upper and lower bounds of error,
Required inputs are
- count: an n x m matrix that will produce n groups each containing m bars.
- errUpperBound: an n x m matrix that defines the upper bound of error for each bar
- errLowerBound: an n x m matrix that defines the lower bound of error for each bar
count = [9862.5 5238 3845.2; 9368.6 3515 3625.7; 11064 6810 4073.6; 12599.2 5701 3955.3; 10114.4 5683 3664.5];
errUpperBound = count + 200;
errLowerBound = count - 100;
errNeg = count - errLowerBound;
errPos = errUpperBound - count;
errorbar(xCnt(:), count(:), errNeg(:), errPos(:), 'k', 'LineStyle','none')
Best Answer