Changing color in legend for grouped bar graph and Minimizing Concise Code

1 view (last 30 days)
Currently I have a legend for my bar graph that shows the colors of the Achilles tendon. But I want the legend to show a dark color (black) for Rupture, gray for Tendinopathy, and Light gray for Healthy. How would I do that in MATLAB? Also, if you look at my code you will notice that it is very repetitive since I am editing each bar. I would greatly appreciate any advice on how to minimize this code, maybe by using a for loop?
groups = categorical({'Achilles','LG','MG','Soleus'});
hold on
figure(1)
CSA_graph = bar(groups, avgCSA)
title('Average CSA for Triceps Surae and Achilles Tendon')
xlabel("Tissue Name")
ylabel('Average CSA (mm^2)')
legend('Achilles Rupture','Achilles Tendinopathy','Healthy',Location='west')
for a=1:3
CSA_graph(a).FaceColor = 'flat';
end
CSA_graph(1).CData(1,:) = [hex2rgb('#556EC1')];
CSA_graph(2).CData(1,:) = [hex2rgb('#648FFF')];
CSA_graph(3).CData(1,:) = [hex2rgb('#9AB6FF')];
CSA_graph(1).CData(2,:) = [hex2rgb('#98295D')];
CSA_graph(2).CData(2,:) = [hex2rgb('#DC267F')];
CSA_graph(3).CData(2,:) = [hex2rgb('#EC87B9')];
CSA_graph(1).CData(3,:) = [hex2rgb('#B9521A')];
CSA_graph(2).CData(3,:) = [hex2rgb('#FE6100')];
CSA_graph(3).CData(3,:) = [hex2rgb('#FE9858')];
CSA_graph(1).CData(4,:) = [hex2rgb('#337E21')];
CSA_graph(2).CData(4,:) = [hex2rgb('#1CC214')];
CSA_graph(3).CData(4,:) = [hex2rgb('#82D97E')];
hold off
  1 Comment
dpb
dpb on 2 Aug 2024
@Meghna Raj, it would be helpful if you would attach the data; just "save avgCSA avgCSA" and then attach the .mat file..are the other colors what you want; only changing those for the first set? If so, simply find a color triplet you like and replace the current values with those. Solid black is [0 0 0]; a common light gray is 0.9*[1 1 1]; adjust to whatever level of darkness suits.
You could write the above in a for loop, yes, but you would have to then store the color data in an array by group and condition and index into that array; once you've got the above written unless there are going to be alternate numbers of groups and/or conditions per group, it would hardly seem worth the effort; once you package this in a function that gets called, it's "out of sight, out of mind" and doesn't need anything but to pass it the data array...

Sign in to comment.

Answers (1)

dpb
dpb on 2 Aug 2024
Moved: dpb on 2 Aug 2024
MAX=[200, 900, 1200, 2000]; % a max to generate sample data
MIN=[ 50, 600, 900, 1800]; % and a min
for i=1:numel(MAX)
avgCSA(i,:)=randi([MIN(i) MAX(i)],1,3);
end
groups = categorical({'Achilles','LG','MG','Soleus'});
hold on
figure(1)
CSA_graph = bar(groups, avgCSA);
title('Average CSA for Triceps Surae and Achilles Tendon')
xlabel("Tissue Name")
ylabel('Average CSA (mm^2)')
legend('Achilles Rupture','Achilles Tendinopathy','Healthy',Location='northwest')
%for a=1:3
% CSA_graph(a).FaceColor = 'flat';
%end
set(CSA_graph,{'FaceColor'},{'flat'}); % can use multiple-handle facility of set
SHADES=[0 0.5 0.9]; % define some shades of gray...
CSA_graph(1).CData(1,:) = SHADES(1)*[1 1 1];
CSA_graph(2).CData(1,:) = SHADES(2)*[1 1 1];
CSA_graph(3).CData(1,:) = SHADES(3)*[1 1 1];
CSA_graph(1).CData(2,:) = [hex2rgb('#98295D')];
CSA_graph(2).CData(2,:) = [hex2rgb('#DC267F')];
CSA_graph(3).CData(2,:) = [hex2rgb('#EC87B9')];
CSA_graph(1).CData(3,:) = [hex2rgb('#B9521A')];
CSA_graph(2).CData(3,:) = [hex2rgb('#FE6100')];
CSA_graph(3).CData(3,:) = [hex2rgb('#FE9858')];
CSA_graph(1).CData(4,:) = [hex2rgb('#337E21')];
CSA_graph(2).CData(4,:) = [hex2rgb('#1CC214')];
CSA_graph(3).CData(4,:) = [hex2rgb('#82D97E')];
hold off

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!