How do I order my x axis in bar chart

조회 수: 2 (최근 30일)
Andrew Holloway-Breward
Andrew Holloway-Breward 2021년 6월 15일
답변: Andrew Holloway-Breward 2021년 6월 15일
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
Andrew Holloway-Breward 2021년 6월 15일
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