How can I assign similar colors to match two grouped bar chart?
2 views (last 30 days)
Show older comments
MD MEHEDI HASSAN
on 17 Mar 2025
Commented: Star Strider
on 18 Mar 2025
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');
% 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:');
disp(array2table(mean(values), 'VariableNames', categories));
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');
0 Comments
Accepted Answer
Star Strider
on 17 Mar 2025
Edited: Star Strider
on 17 Mar 2025
Try this —
% MATLAB Live Script to plot a bar graph from CSV data
% Read the data from CSV file
data = readtable('HealthyvsOA_BSPC.csv');
% 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:');
disp(array2table(mean(values), 'VariableNames', categories));
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.
.
2 Comments
More Answers (0)
See Also
Categories
Find more on 2-D and 3-D Plots 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!


