Plotting error bars on grouped bar
28 views (last 30 days)
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:
0 Comments
Accepted Answer
the cyclist
on 24 May 2024
You need the XEndPonits property of the bars.
% Sample data
data = [1.5, 2.3, 3.2; 2.0, 2.8, 3.5; 1.8, 2.5, 3.0];
errors = [0.2, 0.3, 0.4; 0.3, 0.2, 0.3; 0.2, 0.3, 0.2];
% Number of groups and number of bars in each group
[numGroups, numBars] = size(data);
% Plot the grouped bar chart
figure;
hold on;
hBar = bar(data);
% Get the x positions of the bars
xBar = nan(numGroups, numBars);
for i = 1:numBars
xBar(:, i) = hBar(i).XEndPoints;
end
% Plot the error bars
for i = 1:numGroups
for j = 1:numBars
errorbar(xBar(i, j), data(i, j), errors(i, j), 'k', 'linestyle', 'none', 'CapSize', 10);
end
end
% Labels and title
xlabel('Group');
ylabel('Value');
title('Grouped Bar Chart with Error Bars');
legend('Bar 1', 'Bar 2', 'Bar 3');
hold off;
2 Comments
the cyclist
on 27 May 2024
Sorry for the delayed reply. I did not get notified of your comment.
I have no issue adding axis labels. Here is the same code above, with labels added. If you cannot get it to work, I suggest opening a new question, and marking this one as resolved, since it solved your original question.
% Sample data
data = [1.5, 2.3, 3.2; 2.0, 2.8, 3.5; 1.8, 2.5, 3.0];
errors = [0.2, 0.3, 0.4; 0.3, 0.2, 0.3; 0.2, 0.3, 0.2];
% Number of groups and number of bars in each group
[numGroups, numBars] = size(data);
% Plot the grouped bar chart
figure;
hold on;
hBar = bar(data);
% Get the x positions of the bars
xBar = nan(numGroups, numBars);
for i = 1:numBars
xBar(:, i) = hBar(i).XEndPoints;
end
% Plot the error bars
for i = 1:numGroups
for j = 1:numBars
errorbar(xBar(i, j), data(i, j), errors(i, j), 'k', 'linestyle', 'none', 'CapSize', 10);
end
end
% Labels and title
xlabel('Group');
ylabel('Value');
title('Grouped Bar Chart with Error Bars');
legend('Bar 1', 'Bar 2', 'Bar 3');
hold off;
set(gca,"XTick",[1 2 3],"XTickLabel",["yes","no","maybe"])
More Answers (0)
See Also
Categories
Find more on Histograms 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!