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

조회 수: 2 (최근 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

추가 답변 (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 CenterFile Exchange에서 Detection에 대해 자세히 알아보기

태그

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by