error bar on group plot :X-data must be the same size as Y-data.

조회 수: 5 (최근 30일)
% Set up the problem by recreating the table.
A = [ 0 0 1 1 2 2 3 3]';
B = ['0' '1' '0' '1' '0' '1' '0' '1']';
C = [2 11 32 33 38 31 47 39]';
D = [0.5 0.7 9.5 3.3 3.8 3.1 4.7 3.9]';
T = table(A,B,C,D);
% use sortrows to organize data in an expected format
T = sortrows(T,["B","A"]);
% extract x values, group names
x = unique(T.A);
b = unique(T.B);
% Reshape the data based on the number of unique values in A
CC = reshape(T.C,length(x),[])
DD = reshape(T.D,length(x),[])
% plot
hold on
bar(x,CC)
errorbar(x,CC,DD)
legend(b)
hold off
Error using errorbar (line 105)
X-data must be the same size as Y-data.

채택된 답변

Alan Stevens
Alan Stevens 2022년 8월 27일
Something like this?
% Set up the problem by recreating the table.
A = [ 0 0 1 1 2 2 3 3]';
B = ['0' '1' '0' '1' '0' '1' '0' '1']';
C = [2 11 32 33 38 31 47 39]';
D = [0.5 0.7 9.5 3.3 3.8 3.1 4.7 3.9]';
T = table(A,B,C,D);
% use sortrows to organize data in an expected format
T = sortrows(T,["B","A"]);
% extract x values, group names
x = unique(T.A);
b = unique(T.B);
% Reshape the data based on the number of unique values in A
CC = reshape(T.C,length(x),[]);
DD = reshape(T.D,length(x),[]);
% plot
hold on
bar(x,CC)
errorbar(x-0.15,CC(:,1),DD(:,1),'o')
errorbar(x+0.15,CC(:,2),DD(:,2),'o')
legend(b)
hold off
  댓글 수: 7
Frederick Awuah-Gyasi
Frederick Awuah-Gyasi 2022년 9월 7일
How do I add text labels over the bars.
% Set up the problem by recreating the table.
A = [ '0' '0' '1' '1' '2' '2' '3' '3']'; %%%%%%%%%%%%%%%
B = ['0' '1' '0' '1' '0' '1' '0' '1']';
C = [2 11 32 33 38 31 47 39]';
D = [0.5 0.7 9.5 3.3 3.8 3.1 4.7 3.9]';
E = ["Q1" "Q2" "Q1" "Q4" "Q1" "Q1" "Q2" "Q1"]';
T = table(A,B,C,D,E);
% use sortrows to organize data in an expected format
T = sortrows(T,["B","A"]);
% extract x values, group names
x = str2num(unique(T.A)); %%%%%%%%%%%%%%%%%
b = unique(T.B);
% Reshape the data based on the number of unique values in A
CC = reshape(T.C,length(x),[]);
DD = reshape(T.D,length(x),[]);
EE = reshape(T.E,length(x),[]);
% plot
hold on
hb = bar(x,CC)
errorbar(x-0.15,CC(:,1),DD(:,1),'o','Color','k') %%%%%%%%%%%%%%
errorbar(x+0.15,CC(:,2),DD(:,2),'o','Color','k') %%%%%%%%%%%%%%
% legend(b)
% xtips2 = ba(2).XEndPoints;
% ytips2 = ba(2).YEndPoints;
% labels2 = string(EE(2).YData);
%
% text(xtips2,ytips2,labels2,'HorizontalAlignment','center',...
% 'VerticalAlignment','bottom')
barWidth = hb.BarWidth;
numCol = size(CC,1);
cnt = 0;
for ii = numbersToAdd'
cnt = cnt + 1;
xPos = linspace(cnt - barWidth/2, cnt + barWidth / 2, numCol+1);
idx = 1;
for jj = xPos(1:end-1)
val = EE(cnt,idx);
y = M(cnt,idx);
text(jj, y + 1, num2str(val));
idx = idx +1;
end
end
hold off
Alan Stevens
Alan Stevens 2022년 9월 7일
Do you mean something like this?
% Set up the problem by recreating the table.
A = [ '0' '0' '1' '1' '2' '2' '3' '3']'; %%%%%%%%%%%%%%%
B = ['0' '1' '0' '1' '0' '1' '0' '1']';
C = [2 11 32 33 38 31 47 39]';
D = [0.5 0.7 9.5 3.3 3.8 3.1 4.7 3.9]';
E = ["Q1" "Q2" "Q1" "Q4" "Q1" "Q1" "Q2" "Q1"]';
T = table(A,B,C,D,E);
% use sortrows to organize data in an expected format
T = sortrows(T,["B","A"]);
% extract x values, group names
x = str2num(unique(T.A)); %%%%%%%%%%%%%%%%%
b = unique(T.B);
% Reshape the data based on the number of unique values in A
CC = reshape(T.C,length(x),[]);
DD = reshape(T.D,length(x),[]);
EE = reshape(T.E,length(x),[]);
% plot
hold on
hb = bar(x,CC);
errorbar(x-0.15,CC(:,1),DD(:,1),'o','Color','k'); %%%%%%%%%%%%%%
errorbar(x+0.15,CC(:,2),DD(:,2),'o','Color','k'); %%%%%%%%%%%%%%
text(x-0.2,CC(:,1)+DD(:,1)+5,EE(:,1))
text(x+0.1,CC(:,2)+DD(:,2)+5,EE(:,2))
hold off

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by