bar figure with stacked

1 Ansicht (letzte 30 Tage)
Brave A
Brave A am 3 Dez. 2019
Kommentiert: Adam Danz am 4 Dez. 2019
y = [11 4 3;8 7 2;11 2 3;7 4 5; 12 2 2;10 3 3; 10 3 1; 7 5 2;11 1 1;4 5 3;7 3 1];
x = categorical({'Python','Java',' C++','Matlab', 'C','C#','Visual Basic','Processing','R','i','o'});
x = reordercats(x,{'Python','Java',' C++','Matlab', 'C','C#','Visual Basic','Processing','R','p','l'});
bar(x,y,'stacked');
Hi I would like to have stacked for y and x also.
Would you help me in that because it gave error.
Thanks in advance
  2 Kommentare
Star Strider
Star Strider am 3 Dez. 2019
The error is:
Error using categorical/reordercats (line 38)
NEWORDER must be a permutation of the existing categories.
The obvious solution is not to introduce new categories.
Brave A
Brave A am 3 Dez. 2019
Could explain more?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Adam Danz
Adam Danz am 4 Dez. 2019
Bearbeitet: Adam Danz am 4 Dez. 2019
You've got an error/typo in reordercats().
x = categorical({'Python','Java',' C++','Matlab', 'C','C#','Visual Basic','Processing','R','i','o'});
x = reordercats(x,{'Python','Java',' C++','Matlab', 'C','C#','Visual Basic','Processing','R','p','l'});
% HERE----------------------------------------------------------------------------------------^---^
You can't rename the categories. You can only specify their order.
This works, below.
y = [11 4 3;8 7 2;11 2 3;7 4 5; 12 2 2;10 3 3; 10 3 1; 7 5 2;11 1 1;4 5 3;7 3 1];
x = categorical({'Python','Java',' C++','Matlab', 'C','C#','Visual Basic','Processing','R','i','o'});
x = reordercats(x,{'Python','Java',' C++','Matlab', 'C','C#','Visual Basic','Processing','R','i','o'});
% ------------------------------------------------------------------------or maybe you want 'o','i'}); ??
bar(x,y,'stacked');
  6 Kommentare
Brave A
Brave A am 4 Dez. 2019
y = [ 11 4 3;11 2 3 ; 11 1 1; 10 3 1;7 3 1;7 5 2; 12 2 2;10 3 3; 4 5 3 ;8 7 2 ;7 4 5];
% [ ]
[sd,r]=sort(y,'descend')
x = categorical({'Pointers','Basic function calling/program flow','Variable/function declarations','Operators and precedence','Conditional operations','Looping operations ','Scope of variables','Input/and file handling','Arrays','Recursion','String handling'})
bar(x,[sd,r],'stacked'
I tried thid but it's repeat the values and not sorting them.
Adam Danz
Adam Danz am 4 Dez. 2019
y is a matrix with 3 columns. What are you sorting? If you're sorting by stacked bar height, you need to sum the rows of y and this will result in the same order as what you've got already.
y = [11 4 3;8 7 2;11 2 3;7 4 5; 12 2 2;10 3 3; 10 3 1; 7 5 2;11 1 1;4 5 3;7 3 1];
x = categorical({'Python','Java',' C++','Matlab', 'C','C#','Visual Basic','Processing','R','i','o'});
[~,r]=sort(sum(y,2),'descend'); % Sum rows of y
x = reordercats(x,cellstr(x(r))); % use reordercats()
bar(x,y(r,:),'stacked');

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by