Bar Graph from a table : Error Xdata must be unique

A B C
0 '0' 2
0 '1' 11
1 '0' 32
1 '1' 33
2 '0' 38
2 '1' 31
3 '0' 47
3 '1' 39
Want to have C on the Y axis, X axis : 0,1,2,3 , bars colored for B : 0,1.

댓글 수: 4

Welcome to the forum @Frederick Awuah-Gyasi.
As the error message indicates, you have multiple x-values so Matlab doesn't know what to do with it, and neither do we 🙂.
Do you want 2 sets of bar plots?
Do you want to somehow combine the Y data for the same x-data groups?
Something else?
Frederick Awuah-Gyasi
Frederick Awuah-Gyasi 2022년 2월 9일
편집: Frederick Awuah-Gyasi 2022년 2월 9일
@Cris LaPierre This is what I treid b = bar(T.A,T.C) not sure how to pass in T.B.
@Adam DanzI want it grouped B grouped by A on the x axis all using the same Ydata.
Frederick Awuah-Gyasi
Frederick Awuah-Gyasi 2022년 2월 9일
편집: Frederick Awuah-Gyasi 2022년 2월 9일
Thanks for your help @Cris LaPierre and @Adam Danz
A little background :This dataset is a subset of a bigger table got with this code below:
T = groupsummary(BigT,{'A','B'}) so C is the GroupCount

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

 채택된 답변

For bar, groups are created organizing the data into a matrix. The rows correspond to x, and the columns correspond to each data series. See this example.
That means you will need to do some reorganizing of the data to get it formatted correctly to create a grouped bar chart.
% 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]';
T = table(A,B,C);
% 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),[])
CC = 4×2
2 11 32 33 38 31 47 39
% plot
bar(x,CC)
legend(b)

댓글 수: 3

@Cris LaPierre Really do appreciate your quick response and break down of answers. Looking forwarded to be able to contribute to this forum like you.
@Frederick Awuah-Gyasi, I'm looking forward to your contributions!
@Adam Danz challenge accepted.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Networks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by