フィルターのクリア

Bar Graph from a table : Error Xdata must be unique

67 ビュー (過去 30 日間)
Frederick Awuah-Gyasi
Frederick Awuah-Gyasi 2022 年 2 月 9 日
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 件のコメント
Frederick Awuah-Gyasi
Frederick Awuah-Gyasi 2022 年 2 月 9 日
編集済み: Frederick Awuah-Gyasi 2022 年 2 月 9 日
Thanks for your help @Cris LaPierre and @Adam Danz
Frederick Awuah-Gyasi
Frederick Awuah-Gyasi 2022 年 2 月 9 日
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

サインインしてコメントする。

採用された回答

Cris LaPierre
Cris LaPierre 2022 年 2 月 9 日
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 件のコメント
Adam Danz
Adam Danz 2022 年 2 月 9 日
@Frederick Awuah-Gyasi, I'm looking forward to your contributions!
Frederick Awuah-Gyasi
Frederick Awuah-Gyasi 2022 年 2 月 9 日
@Adam Danz challenge accepted.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by