How do I change individual bar colors on "grouped" bar graph?

Can anyone recommend how I can change the individual colors on my grouped bar graph? I have two groups 'Concrete' and 'Abstract' representing different order conditions from my experiment. I'm trying to create a bar graph to visualize differences between two different time durations within the same group. I'm having trouble changing the color on the bars to match with the rest of the color scheme I'm working with. I would like to have the first bar in the first group-opaque blue, second bar/first group-blue, first bar second group-opaque red, second bar second group-red. Below is the script I'm running. I was also having trouble with the legend.
%%%Experiment 2 Bar Graph%%%
%%%Produces visual comparison of alpha values from all participants averaged
%%%for each condition.
%Loads files needed for bar graph
load('Exp2_Allsubs_Ultra_Params.mat')
%Creates standard error for error bars
cond0time1_std_err = std(all_subj_alphas_cond0_time1)./sqrt(length(all_subj_alphas_cond0_time1));
cond0time2_std_err = std(all_subj_alphas_cond0_time2)./sqrt(length(all_subj_alphas_cond0_time2));
cond1time1_std_err = std(all_subj_alphas_cond1_time1)./sqrt(length(all_subj_alphas_cond1_time1));
cond1time2_std_err = std(all_subj_alphas_cond1_time2)./sqrt(length(all_subj_alphas_cond1_time2));
%Creates Bar Graph for Alphaa Parameter
mean_alpha_values = [allsubs_ultraalpha_cond0time1 allsubs_ultraalpha_cond0time2; allsubs_ultraalpha_cond1time1 allsubs_ultraalpha_cond1time2];
alpha_stderr =[cond0time1_std_err cond0time2_std_err; cond1time1_std_err cond1time2_std_err];
y = bar(mean_alpha_values,'grouped');
hold on
% Calculate the number of groups and number of bars in each group
[ngroups,nbars] = size(mean_alpha_values);
% Get the x coordinate of the bars
x = nan(nbars, ngroups);
for i = 1:nbars
x(i,:) = y(i).XEndPoints;
end
% Plot the errorbars
errorbar(x',mean_alpha_values, alpha_stderr,'k','linestyle','none', 'LineWidth', 2);
%Formats Title of Chart
title('Alpha Parameter','FontSize',24,...
'FontName','Times New Roman');
% Create Y-Axis Label
ylabel({'Parameter Value'},'FontSize',18,...
'FontName','Times New Roman');
% %Creates Legend
% legend(bar(y),{'Short','Long'},'Location','northeastoutside')
% xlabel({'Condition Type'},'FontSize',18,'FontName','Times New Roman');
name={'Concrete First';'Abstract First'};
set(gca,'xticklabel',name);
%Formats gcf position
x0=10;
y0=10;
width=500;
height=700;
%Formats Current Axis and Current Figure
set(gca, 'FontSize', 18, 'FontWeight', 'bold')
set(gcf, 'Color', 'w', 'position',[x0,y0,width,height])

 Accepted Answer

The colors come from the colororder. One option is to specify the color order to be what you want.
Another option is to set the colors of each group's bars manually. See this example.
To change the color of each bar individually (ignoring the groups), this example may help.
Combining the information from both examples, your code might look like this (colors are made up):
y = [10 15; 30 35];
b = bar(y);
b(1).FaceColor = 'flat';
b(1).CData(1,:) = [0 0.3059 0.6157];
b(2).FaceColor = 'flat';
b(2).CData(1,:) = [0 0 1];
b(1).CData(2,:) = [0.8078 0.1098 0.2667];
b(2).CData(2,:) = [1 0 0];

4 Comments

I ended up getting the colors and opacity that I wanted on the desired bars, but am a little confused on the correlation between the numbers and the group/columns. Would you mind explaining how the numbers reference specific columns so I better understand for future applications? For example, the first number b(1) refers to the column or group? The second (1,:)?
I determined what the numbers should be by guessing and checking. Based on those observations, my understanding is the following.
  • b(1) correspondes to the first data series, which is the first bar in each grouping
  • b(2) correspondes to the second data series, which is the second bar in each grouping
  • CData(1,:) refers to the first bar in the corresponding data series. The first bar is in the first grouping of 2 (on the left)
  • CData(2,:) refers to the second bar in the corresponding data series. The second bar is in the second grouping of 2 (on the right)
Based on that, b(1).CData(2,:) points to the first data series (first bar in each grouping) but specifically to the 2nd bar in this series (so the first bar in the second grouping).

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!