Plotting error bars on grouped bar
Show older comments
R2023b: I am trying to plot error bars on my grouped bar plot. I was able to generate the error bars, however they are offset from the actual bar. I tried two sets of code:
first code:
figure(1); clf;
hb = bar(y); % get the bar handles
hold on;
for k = 1:size(y,2)
% get x positions per group
xpos = hb(k).XData + hb(k).XOffset;
% draw errorbar
errorbar(xpos, y(:,k), err(:,k), 'LineStyle', 'none', ...
'Color', 'k', 'LineWidth', 1);
end
legend('Boat wake','Wind wave');
set(gca,'xticklabel',{'September Wind'; 'March Boat';'March Wind'; 'May Boat';'May Wind'; 'August Boat'});
second code:
figure(1)
hBar = bar(x,y); % Plot Data, Get Handle
hold on
for k1 = 1:length(hBar) % Loop: Plots Error Bars
hb = get(get(hBar(k1)));
midbar = mean(hb);
errorbar(midbar, y(:,k1), errs(:,k1), '.') % plotting errors
sigbarx(k1,:) = midbar; % Use To Plot Significance Bars
end
The figure I generated:

Accepted Answer
More Answers (0)
Categories
Find more on Bar Plots 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!
