Is there a simple method to exclude empty categories from charts
10 views (last 30 days)
Show older comments
I ran into this same problem with some of my charts. I take a subset of the data, but the chart shows empty collumns for those categories that are not in the subset.
Is there a simple way to remove those categories, that does not require knowing what categories are not included ahead of time?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1268670/image.jpeg)
If I want to drill down by some other category or variable that excludes some of the x-axis categories, I get empty columns.
(as an example only) This example narrows the data set by filtering for Measure below a certain value. E006 and E011 are empty.
I could remove those from the graph by duplicating my dataset then removing the empty categories from that new data set. Or create a new category in the existing dataset and remove. Both of those would be manual, and sound excessive, especially for my somewhat large dataset.
Any recommendations are appreciated.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1268675/image.jpeg)
'E006' 9.15016708650277
'E006' 8.28535569418754
'E006' 8.72069251107633
'E007' 0.155780009337454
'E007' 1.75058230426194
'E007' 1.95525115217540
'E011' 9.34478612596362
'E011' 8.15267787081526
'E011' 8.19832540574649
'E019' 3.47413644178545
'E019' 4.83789297590307
'E019' 6.05565471598406
'E048' 0.669831465921815
'E048' 0.788226485451541
'E048' 9.10088928273200
'E095' 2.01208907965411
'E095' 6.17501425907603
'E095' 6.42923489793257
'E096' 3.67538382824140
'E096' 3.78440478175663
'E096' 5.50000882718926
'E097' 1.94313684575130
'E097' 2.20222031651597
'E097' 5.51352713031426
'EK91' 0.662955883167562
'EK91' 2.42123810665894
'EK91' 5.41126855732457
'EK92' 3.12963039329721
'EK92' 3.83627419164329
'EK92' 5.57952593218829
'EK93' 1.30810431740758
'EK93' 5.99084211811400
'EK93' 7.87485387137294
0 Comments
Accepted Answer
dpb
on 19 Jan 2023
It's a little extra code to write, but it should be able to be made generic -- follow the lead of @Cris LaPierre in that thread; wrap the categorical argument in your plotting routine inside removecats and use a logical addressing vector to address the elements left after your selection.
For the above example data;
E=categorical(C(:,1));
M=[C{:,2}];
ix=(M<=8);
scatter(removecats(E(ix)),M(ix))
results in the same figure as yours above as far as data but without the empty categories (and using plain vanilla scatter, of course).
If you're programmatically setting the limits or other selection criteria, it should be feasible to compute the required indexing variable(s) programmatically and avoid the manual intervention.
0 Comments
More Answers (0)
See Also
Categories
Find more on Get Started with Signal Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!