How do I order my x axis in bar chart

2 次查看(过去 30 天)
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);

采纳的回答

Andrew Holloway-Breward
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);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Bar Plots 的更多信息

标签

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by