How do I order my x axis in bar chart

2 views (last 30 days)
Hi,
I have been trying to work this out for an hour or two and cannot find any answer online to what seems to be a very simple requirement.
Below is my code for determining what data is missing from my dataset, which works perfectly however how do I resequence the bar chart so that the features with most missing data appear first?
The bar chart should display the elements in descending order based upon the values contained within totals.
clear;
clc;
T = readtable('data/car_insurance_claim.csv');
r = zeros(height(T),width(T));
mt = {'' NaN Inf};
for col=1:width(T)
features = T(:,col);
m = ismissing(features, mt);
r(:,col) = m;
end
totals = sum(r);
labels = categorical(T.Properties.VariableNames);
bar(labels, totals);

Accepted Answer

Andrew Holloway-Breward
Andrew Holloway-Breward on 15 Jun 2021
OK, as per usual a cup of tea and a 10 minute break and I have managed to achieve what I wanted to achieve, this might help somebody else in the future:
clear;
clc;
T = readtable('data/car_insurance_claim.csv');
r = zeros(height(T),width(T));
mt = {'' NaN Inf};
for col=1:width(T)
features = T(:,col);
m = ismissing(features, mt);
r(:,col) = m;
end
totals = sum(r);
[totalsSorted, index] = sort(totals, 'descend');
labels = categorical(T.Properties.VariableNames);
labelsSorted = labels(index);
labelsSorted = reordercats(labelsSorted,string(labelsSorted));
bar(labelsSorted, totalsSorted);

More Answers (0)

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!