How to apply individual color to each bar on a bar chart plot

3 ビュー (過去 30 日間)
Ahmad
Ahmad 2024 年 5 月 4 日
コメント済み: cui,xingxing 2024 年 5 月 5 日
The code is working perfect, but all the bars are showing blue color by default, I want each bar to have a diffrent color. The code is divided into two files: The function part and the normal code
The Function file
function plotResults(Cleveland_accuracy, Cleveland_sensitivity, Cleveland_specificity, Cleveland_precision, Cleveland_fScore, Cleveland_mcc, Public_Health_Dataset_accuracy, Public_Health_Dataset_sensitivity, Public_Health_Dataset_specificity, Public_Health_Dataset_precision, Public_Health_Dataset_fScore, Public_Health_Dataset_mcc, Jindong_accuracy, Jindong_sensitivity, Jindong_specificity)
figure
cats = categorical({'Cleveland Dataset','Public Health Dataset'});
cats = reordercats(cats, {'Cleveland Dataset','Public Health Dataset'});
results = [Cleveland_accuracy Cleveland_sensitivity Cleveland_specificity Cleveland_precision Cleveland_fScore Cleveland_mcc;...
Public_Health_Dataset_accuracy Public_Health_Dataset_sensitivity Public_Health_Dataset_specificity Public_Health_Dataset_precision Public_Health_Dataset_fScore Public_Health_Dataset_mcc];
bar(cats, results);
ylabel('Metric Value (%)');
legend('Accuracy', 'Sensitivity', 'Specificity', 'Precision', ...
'F1-Score', 'Matthews correlation coefficient', 'Location', 'northoutside');
% Accuracy comparison for the three model
figure
cats = categorical({'Cleveland Model', 'Jindong et al Model','Public Health Model'});
cats = reordercats(cats, {'Cleveland Model', 'Jindong et al Model','Public Health Model'});
results = [Cleveland_accuracy ;...
Jindong_accuracy;...
Public_Health_Dataset_accuracy];
bar(cats, results);
ylabel('Accuracy Values (%)');
legend('Accuracy', ...
'Location', 'northoutside');
% Sensitivity comparison for the three model
figure
cats = categorical({'Cleveland Model', 'Jindong et al Model','Public Health Model'});
cats = reordercats(cats, {'Cleveland Model', 'Jindong et al Model','Public Health Model'});
results = [Cleveland_sensitivity ;...
Jindong_sensitivity;...
Public_Health_Dataset_sensitivity];
bar(cats, results);
ylabel('Sensitivity Values (%)');
legend('Sensitivity', 'Location', 'northoutside');
% SPecificity comparison for the three model
figure
cats = categorical({'Cleveland Model', 'Jindong et al Model','Public Health Model'});
cats = reordercats(cats, {'Cleveland Model', 'Jindong et al Model','Public Health Model'});
results = [Cleveland_specificity ;...
Jindong_specificity;...
Public_Health_Dataset_specificity];
bar(cats, results);
ylabel('SPecificity Values (%)');
legend('SPecificity', 'Location', 'northoutside');
end
%To call the fuction we have the following codenin a separate file
% Metric values for the main model
Cleveland_accuracy = 99;
Cleveland_sensitivity = 97;
Cleveland_specificity = 100;
Cleveland_precision = 100;
Cleveland_fScore = 98;
Cleveland_mcc = 79;
% Metrics values for Jindong Feng et al model
Jindong_accuracy = 91.25;
Jindong_sensitivity = 91.54;
Jindong_specificity = 90.32;
% Metric values for the combined dataset model
Public_Health_Dataset_accuracy = 87;
Public_Health_Dataset_sensitivity = 77;
Public_Health_Dataset_specificity = 98;
Public_Health_Dataset_precision = 98;
Public_Health_Dataset_fScore = 86;
Public_Health_Dataset_mcc = 75;
% Call the plotResults function with metric values
plotResults(Cleveland_accuracy, Cleveland_sensitivity, Cleveland_specificity, ...
Cleveland_precision, Cleveland_fScore, Cleveland_mcc, Public_Health_Dataset_accuracy, Public_Health_Dataset_sensitivity, Public_Health_Dataset_specificity, Public_Health_Dataset_precision, Public_Health_Dataset_fScore, Public_Health_Dataset_mcc, Jindong_accuracy, Jindong_sensitivity, Jindong_specificity);

採用された回答

cui,xingxing
cui,xingxing 2024 年 5 月 4 日
編集済み: cui,xingxing 2024 年 5 月 4 日
Example:
%To call the fuction we have the following codenin a separate file
% Metric values for the main model
Cleveland_accuracy = 99;
Cleveland_sensitivity = 97;
Cleveland_specificity = 100;
Cleveland_precision = 100;
Cleveland_fScore = 98;
Cleveland_mcc = 79;
% Metrics values for Jindong Feng et al model
Jindong_accuracy = 91.25;
Jindong_sensitivity = 91.54;
Jindong_specificity = 90.32;
% Metric values for the combined dataset model
Public_Health_Dataset_accuracy = 87;
Public_Health_Dataset_sensitivity = 77;
Public_Health_Dataset_specificity = 98;
Public_Health_Dataset_precision = 98;
Public_Health_Dataset_fScore = 86;
Public_Health_Dataset_mcc = 75;
% Call the plotResults function with metric values
plotResults(Cleveland_accuracy, Cleveland_sensitivity, Cleveland_specificity, ...
Cleveland_precision, Cleveland_fScore, Cleveland_mcc, Public_Health_Dataset_accuracy, Public_Health_Dataset_sensitivity, Public_Health_Dataset_specificity, Public_Health_Dataset_precision, Public_Health_Dataset_fScore, Public_Health_Dataset_mcc, Jindong_accuracy, Jindong_sensitivity, Jindong_specificity);
function plotResults(Cleveland_accuracy, Cleveland_sensitivity, Cleveland_specificity, Cleveland_precision, Cleveland_fScore, Cleveland_mcc, Public_Health_Dataset_accuracy, Public_Health_Dataset_sensitivity, Public_Health_Dataset_specificity, Public_Health_Dataset_precision, Public_Health_Dataset_fScore, Public_Health_Dataset_mcc, Jindong_accuracy, Jindong_sensitivity, Jindong_specificity)
figure
cats = categorical({'Cleveland Dataset','Public Health Dataset'});
cats = reordercats(cats, {'Cleveland Dataset','Public Health Dataset'});
results = [Cleveland_accuracy Cleveland_sensitivity Cleveland_specificity Cleveland_precision Cleveland_fScore Cleveland_mcc;...
Public_Health_Dataset_accuracy Public_Health_Dataset_sensitivity Public_Health_Dataset_specificity Public_Health_Dataset_precision Public_Health_Dataset_fScore Public_Health_Dataset_mcc];
bar(cats, results);
ylabel('Metric Value (%)');
legend('Accuracy', 'Sensitivity', 'Specificity', 'Precision', ...
'F1-Score', 'Matthews correlation coefficient', 'Location', 'northoutside');
% Accuracy comparison for the three model
figure
cats = categorical({'Cleveland Model', 'Jindong et al Model','Public Health Model'});
cats = reordercats(cats, {'Cleveland Model', 'Jindong et al Model','Public Health Model'});
results = [Cleveland_accuracy ;...
Jindong_accuracy;...
Public_Health_Dataset_accuracy];
h =bar(cats, results);
num = numel(results);
colors = rand(num,3);
h.FaceColor = "flat";
% Custom color triad [r,g,b] for each bar, each row corresponds to each
% bar color!
h.CData = colors;
ylabel('Accuracy Values (%)');
legend('Accuracy', ...
'Location', 'northoutside');
% Sensitivity comparison for the three model
figure
cats = categorical({'Cleveland Model', 'Jindong et al Model','Public Health Model'});
cats = reordercats(cats, {'Cleveland Model', 'Jindong et al Model','Public Health Model'});
results = [Cleveland_sensitivity ;...
Jindong_sensitivity;...
Public_Health_Dataset_sensitivity];
h=bar(cats, results);
num = numel(results);
colors = rand(num,3);
h.FaceColor = "flat";
% Custom color triad [r,g,b] for each bar, each row corresponds to each
% bar color!
h.CData = colors;
ylabel('Sensitivity Values (%)');
legend('Sensitivity', 'Location', 'northoutside');
% SPecificity comparison for the three model
figure
cats = categorical({'Cleveland Model', 'Jindong et al Model','Public Health Model'});
cats = reordercats(cats, {'Cleveland Model', 'Jindong et al Model','Public Health Model'});
results = [Cleveland_specificity ;...
Jindong_specificity;...
Public_Health_Dataset_specificity];
h=bar(cats, results);
num = numel(results);
% Custom color triad [r,g,b] for each bar, each row corresponds to each
% bar color!
colors = rand(num,3);
h.FaceColor = "flat";
h.CData = colors;
ylabel('SPecificity Values (%)');
legend('SPecificity', 'Location', 'northoutside');
end
  2 件のコメント
Ahmad
Ahmad 2024 年 5 月 4 日
Thank you @cui,xingxing and @the cyclist for your response.
cui,xingxing
cui,xingxing 2024 年 5 月 5 日
It's my pleasure to help you.

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

その他の回答 (1 件)

the cyclist
the cyclist 2024 年 5 月 4 日
The documentation for the bar command has a section on how to control individual bar colors.

カテゴリ

Help Center および File ExchangeDetection についてさらに検索

タグ

製品


リリース

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by