How to apply individual color to each bar on a bar chart plot
2 views (last 30 days)
Show older comments
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);
Accepted Answer
cui,xingxing
on 4 May 2024
Edited: cui,xingxing
on 4 May 2024
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 Comments
More Answers (1)
See Also
Categories
Find more on Animation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!