MATLAB: How to change colors and generate errorbars in a bar graph

bar graphcolorerrorbar

With the categorical data, how should I:
(1) set two bars into different colors
(2) create errorbars separately
data_1 = [1 2 3 4 5 6];
data_2 = [9 8 7 6 5 4];
mean_1 = mean(data_1);
mean_2 = mean(data_2)
x = categorical({'Variable_1', 'Variable_2'});
y = [mean_1, mean_2];
bar(x,y)
Many thanks!

Best Answer

% Store the bar handle
bh = bar(x,y)
% Set the bar colors
bh.FaceColor = 'flat';
bh.CData = [1 0 0; 0 .5 0]; %red; green
% Set errorbars to +/- 0.5 and 1.0
hold on
eb = errorbar(x,y,[.5,1],'LineStyle','none','Color', 'k');