How to render a bar chart in descending order with x-axis label on each bar?

4 ビュー (過去 30 日間)
I just cannot fathom this out and wondered whether anybody could help before my PC exits the window!
I am trying to plot a bar chart with the values displayed in descending order and each bar has its label shown on the x-axis and nothing I've tried works.
My current code is this: -
clc;
clear;
T = readtable('data/car_insurance_claim_cleanse.csv');
target = table2array(T(:,26));
delCols = [1 3 24 26];
T(:,delCols) = [];
[idx, scores] = fscchi2(T, target);
varnames = T.Properties.VariableNames(idx);
labels = escapeLodash(varnames);
bar(labels, scores(idx));
xlabel('Rank');
ylabel('Score');
and this produces a bar chart like this: -
however as you can see the sequence of the bars is not in descending order?
If I change my code to: -
clc;
clear;
T = readtable('data/car_insurance_claim_cleanse.csv');
target = table2array(T(:,26));
delCols = [1 3 24 26];
T(:,delCols) = [];
[idx, scores] = fscchi2(T, target);
varnames = T.Properties.VariableNames(idx);
labels = escapeLodash(varnames);
bar(scores(idx));
xlabel('Rank');
ylabel('Score');
xticklabels(labels);
I get the columns in the correct sequence however my labels are missing from each bar?
escapeLodash just escapes the underscores for display purposes, but here's the code anyway: -
function r = escapeLodash(labels)
work1=cellstr(labels);
work2 = strrep(work1, '_', '\_');
r = nominal(work2);
end
Any ideas?
  2 件のコメント
Scott MacKenzie
Scott MacKenzie 2021 年 6 月 18 日
It might help if you provide the complete code that generated these charts.
Andrew Holloway-Breward
Andrew Holloway-Breward 2021 年 6 月 18 日
My apologies I thought summarising the bar chart bit was sufficient, but you're right.
I've updated the code as requested.

サインインしてコメントする。

採用された回答

Andrew Holloway-Breward
Andrew Holloway-Breward 2021 年 6 月 18 日
Finally got there, this reordercats function is really confusing and totally intuitive!
clc;
clear;
T = readtable('data/car_insurance_claim_cleanse.csv');
target = table2array(T(:,26));
delCols = [1 3 24 26];
T(:,delCols) = [];
[idx, scores] = fscchi2(T, target);
varnames = T.Properties.VariableNames(idx);
labels = escapeLodash(varnames);
labels = reordercats(labels, string(labels));
bar(labels, scores(idx));
xlabel('Rank');
ylabel('Score');
this produces the chart as follows: -

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLabels and Annotations についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by