How to apply individual color to each bar on a bar chart plot
2 次查看(过去 30 天)
显示 更早的评论
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
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
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Detection 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!