How can I assign similar colors to match two grouped bar chart?

2 views (last 30 days)
I wrote the following code to plot two bar graphs. The first graph represents data from six sensors for four categories of individuals (Healthy, KL_2, KL_3, KL_4), while the second graph represents the mean value of the six sensors for the same four categories. The first graph automatically assigns four different colors to the four categories, but the second graph does not. How can I assign similar colors to the second graph to match the colors used in the first bar chart?
% MATLAB Live Script to plot a bar graph from CSV data
% Read the data from CSV file
data = readtable('HealthyvsOA_BSPC.csv');
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
% Extract sensor names and category values
sensorNames = data.Sensor;
values = data{:, 2:end}; % Numeric data excluding first column
categories = data.Properties.VariableNames(2:end);
% Create bar plot
figure;
bar(values);
% Customize plot
set(gca, 'XTickLabel', sensorNames, 'XTick', 1:numel(sensorNames));
xlabel('Sensor');
ylabel('Average Hits');
% ylim([0 175])
yticks(0:25:175);
legend(categories, 'Location', 'best');
title('Bar Graph of Healthy vs OA Patient Data');
grid off;
% Improve visualization
xtickangle(45);
% Save figure as an image
% saveas(gcf, 'HealthyvsOA_plot.tiff');
% Display summary statistics
disp('Summary Statistics:');
Summary Statistics:
disp(array2table(mean(values), 'VariableNames', categories));
Healthy KL_2 KL_3 KL_4 _______ ____ ____ ______ 16.967 19 80.3 113.13
meanVal = mean(values);
% Create bar plot
figure;
bar(meanVal);
% Customize plot
set(gca, 'XTickLabel', categories);
ylabel('Average Hits');
ylim([0 125])
yticks(0:25:125);
% legend(categories, 'Location', 'best');
title('Bar Graph of Mean Healthy vs OA Patient Data');
grid off;
% Save figure as an image
% saveas(gcf, 'HealthyvsOA_plot.tiff');

Accepted Answer

Star Strider
Star Strider on 17 Mar 2025
Edited: Star Strider on 17 Mar 2025
Use the CData property of each bar.
Try this —
% MATLAB Live Script to plot a bar graph from CSV data
% Read the data from CSV file
data = readtable('HealthyvsOA_BSPC.csv');
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
% Extract sensor names and category values
sensorNames = data.Sensor;
values = data{:, 2:end}; % Numeric data excluding first column
categories = data.Properties.VariableNames(2:end);
% Create bar plot
figure;
hb = bar(values);
for k = 1:numel(hb)
clr{k,:} = hb(k).FaceColor;
end
% Customize plot
set(gca, 'XTickLabel', sensorNames, 'XTick', 1:numel(sensorNames));
xlabel('Sensor');
ylabel('Average Hits');
% ylim([0 175])
yticks(0:25:175);
legend(categories, 'Location', 'best');
title('Bar Graph of Healthy vs OA Patient Data');
grid off;
% Improve visualization
xtickangle(45);
% Save figure as an image
% saveas(gcf, 'HealthyvsOA_plot.tiff');
% Display summary statistics
disp('Summary Statistics:');
Summary Statistics:
disp(array2table(mean(values), 'VariableNames', categories));
Healthy KL_2 KL_3 KL_4 _______ ____ ____ ______ 16.967 19 80.3 113.13
meanVal = mean(values);
% Create bar plot
figure;
hb = bar(meanVal);
hb.FaceColor = 'flat';
for k = 1:numel(hb.XData)
hb.CData(k,:) = clr{k};
end
% Customize plot
set(gca, 'XTickLabel', categories);
ylabel('Average Hits');
ylim([0 125])
yticks(0:25:125);
% legend(categories, 'Location', 'best');
title('Bar Graph of Mean Healthy vs OA Patient Data');
grid off;
% Save figure as an image
% saveas(gcf, 'HealthyvsOA_plot.tiff');
EDIT — Corrected typographical errors.
.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!