Bar Graph Categorisation and Coloring Issue

1 次查看(过去 30 天)
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;

采纳的回答

Voss
Voss 2024-4-4
编辑:Voss 2024-4-4
It's more convenient to put Cat1, Cat2, etc., into a matrix with 8 rows to form y_values, rather than one long row vector, because then you can use one row at a time in the for loop (you were doing one element at a time), and also you have exactly as many colors as rows so you can get rid of the mod() logic.
Then store the created bars in another matrix of the same size (I call it h), and use only the first bar in each group for the legend. Use the 'stable' option in unique to make the legend in the same order as the groups in group_names.
(Also if you want Cat3 through Cat5 to be yellow, the third cell of colors needs to contain 'y', not 'b'.)
% 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 an 8-by-4 matrix
y_values = [Cat1; Cat2; Cat3; Cat4; Cat5; Cat6; Cat7; Cat8];
% Define colors for each group
% colors = {'b', 'b', 'b', 'y', 'y', 'g', 'g', 'g'};
colors = {'b', 'b', 'y', 'y', 'y', 'g', 'g', 'g'}; % make Cat3 yellow, as per instructions
% Define group names for legend
group_names = {'DCZ', 'DCZ', 'DCZ', 'Saline', 'Saline', 'Relax', 'Relax', 'Relax'};
% Plotting
figure;
hold on;
[N,M] = size(y_values);
h = gobjects(N,M);
for i = 1:N
% store the bars created because only certain ones are used in the legend
h(i,:) = bar(i, y_values(i,:), 'FaceColor', colors{i});
end
% Set x-axis labels
xticks(1:N); % 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
[ugn,idx] = unique(group_names,'stable'); % only use the first bar
legend(h(idx,1),ugn, 'Location', 'northwest'); % from each group in the legend
% Add title and labels
title('Column Graph of Values');
xlabel('Date');
ylabel('Values');
hold off;

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by