How do I order my x axis in bar chart

2 visualizaciones (últimos 30 días)
Andrew Holloway-Breward
Andrew Holloway-Breward el 15 de Jun. de 2021
Respondida: Andrew Holloway-Breward el 15 de Jun. de 2021
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);

Respuesta aceptada

Andrew Holloway-Breward
Andrew Holloway-Breward el 15 de Jun. de 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);

Más respuestas (0)

Categorías

Más información sobre Bar Plots en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by