Plotting error bars on grouped bar
조회 수: 22 (최근 30일)
이전 댓글 표시
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
채택된 답변
the cyclist
2024년 5월 24일
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
the cyclist
2024년 5월 27일
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"])
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Histograms에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!