Bar Graph Categorisation and Coloring Issue
Mostra commenti meno recenti
Hi, okay, so I thought this wil be an easy task and I am sincerely sorry if I bother with something overly simple but could anyone please give me a hint on how to accomplish the following ? I would like to plot cat1 and cat2 data columns blue, cat3-cat5 columns yellow and cat6-cat8 columns green. Next I would like to display a legend into the graph with categories DCZ (blue), Saline (yellow) and Rest (green). The code still does not work the way I would like it to, even though the legend displays all 3 categories correctly, it assigns only blue color to all of them. Coloring the columns works incorrectly as well.
Any advice will be immensely appreciated, thanks a lot.
% Define data
Cat1 = [3500, 3756, 4497, 4855];
Cat2 = [4000, 3756, 399, 4855];
Cat3 = [100, 149, 440, 2647];
Cat4 = [10, 91, 2499, 2763];
Cat5 = [3500, 3756, 4497, 4855];
Cat6 = [4365, 5413, 4395, 5006];
Cat7 = [4439, 6362, 5246, 4490];
Cat8 = [3532, 3135, 10380, 7685];
% Joining all values into one vector
y_values = [Cat1, Cat2, Cat3, Cat4, Cat5, Cat6, Cat7, Cat8];
% Define colors for each group
colors = {'b', 'b', 'b', 'y', 'y', 'g', 'g', 'g'};
% Define group names for legend
group_names = {'DCZ', 'DCZ', 'DCZ', 'Saline', 'Saline', 'Relax', 'Relax', 'Relax'};
% Plotting
figure;
hold on;
for i = 1:length(y_values)
bar(i, y_values(i), 'FaceColor', colors{mod(i-1, length(colors)) + 1});
end
% Set x-axis labels
xticks(1:4:length(y_values)); % Placing x-axis ticks for every value
xticklabels({'Nov 11', 'Nov 12', 'Nov 13', 'Nov 14', 'Nov 15', 'Nov 16', 'Nov 17', 'Nov 18'});
% Add legend
legend(unique(group_names), 'Location', 'northwest');
% Add title and labels
title('Column Graph of Values');
xlabel('Date');
ylabel('Values');
hold off;
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Annotations in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

