Plotting error bars on grouped bar
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: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: 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: bar plot, error bars, standard error MATLAB Answers — New Questions