Selecting categorical variable range to plot on bar graph
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I don't understand why the categorical data is shown in its entirety on the graph despite only a portion of it being selected.
Am I indexing it wrongly?
C = categorical({'A'; 'B'; 'C'; 'D'; 'E'; 'F'});
N = [5 4 2 3 4 8];
bar(C(3:end), N(3:end))
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/752574/image.png)
2 commentaires
Siddharth Bhutiya
le 4 Oct 2021
When you create a cateogorical array, you specify the list of categories. That list now becomes a property of the categorical object. Taking a subset of the array using indexing would not update that list. If you want to add/remove a category from your array then you would have to use something like addcats/removecats.
>> C = categorical({'A'; 'B'; 'C'; 'D'; 'E'; 'F'});
>> categories(C)
ans =
6×1 cell array
{'A'}
{'B'}
{'C'}
{'D'}
{'E'}
{'F'}
>> C1 = C(3:end)
C1 =
4×1 categorical array
C
D
E
F
>> categories(C1)
ans =
6×1 cell array
{'A'}
{'B'}
{'C'}
{'D'}
{'E'}
{'F'}
Réponse acceptée
VBBV
le 29 Sep 2021
C = categorical({'A'; 'B'; 'C'; 'D'; 'E'; 'F'});
N = [5 4 2 3 4 8];
bar(C(3:end), N(3:end))
xticks(C(3:end))
xticklabels(C(3:end))
Use Xticks & Xticklabels
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!