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

221 vues (au cours des 30 derniers jours)
Ryan Gallagher
Ryan Gallagher le 28 Oct 2022
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])

Réponse acceptée

Cris LaPierre
Cris LaPierre le 28 Oct 2022
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 commentaires
Ryan Gallagher
Ryan Gallagher le 3 Nov 2022
Thank you
Niki Drossinos
Niki Drossinos le 2 Nov 2023
Thank you so much! :-)

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by