How can I assign similar colors to match two grouped bar chart?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
MD MEHEDI HASSAN
le 17 Mar 2025
Commenté : Star Strider
le 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 commentaires
Réponse acceptée
Star Strider
le 17 Mar 2025
Modifié(e) : Star Strider
le 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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



