필터 지우기
필터 지우기

putting error bars on bar plot?

조회 수: 125 (최근 30일)
nines
nines 2021년 11월 9일
댓글: Star Strider 2021년 11월 9일
Hello!
I have a mean of subjects bar plot, specifically a horizontal bar plot (barh), and I want to add error bars to the the bar plot and plot it against categorical data. I have calculated the STD:
values:
std = 34x1 double
mean = 34x1 double
x = 1x34 categorical
std = std(matrix, 0, 2)
barwitherr(std, 1:length(x), mean_matrix)
The plot runs, but doesn't add error bars.
I am getting the error:
Undefeined function barwitherr for input arguments of type double.
Can you help me?

채택된 답변

Star Strider
Star Strider 2021년 11월 9일
I do not remember when ‘XEndPoints’ and ‘YEndPoints’ were introduced (and I am not able to find it in the documentation), so I included two options —
x = categorical({'a','b','c'});
y = [75.8 78.05; 81 80.30; 91 80.78];
err = rand(size(y))*10;
figure
b = bar(x,y);
hold on
for k = 1:numel(b) % Recent MATLAB Versions
xtips = b(k).XEndPoints;
ytips = b(k).YEndPoints;
errorbar(xtips,ytips,err(:,k), '.g', 'MarkerSize',0.1)
end
hold off
figure
b = bar(y);
for k = 1:numel(b) % Earlier MATLAB Versions
ctr(k,:) = bsxfun(@plus, b(k).XData, [b(k).XOffset]');
ydt(k,:) = b(k).YData;
end
hold on
errorbar(ctr, ydt, err.', '.g', 'MarkerSize',0.1)
hold off
set(gca,'XTickLabel',x)
This is a general solution, using single or grouped bar plots, and adapts to the size of ‘y’. Choose the approach that works, depending on the available MATLAB version/release.
.
  댓글 수: 2
nines
nines 2021년 11월 9일
thank you!
Star Strider
Star Strider 2021년 11월 9일
As always, my pleasure!
.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by