ABC=[ [10,13,9,6,20]' [2, 4, 7, 9,10]' [-2,-3,-8,-10,-11]'].';
hBar=bar(ABC,'grouped');
hFg=gcf; hAx=gca;
ylim([-20 30])
hAx.XTickLabel={'A';'B';'C'};
hAx.YColor=get(hFg,'color'); box off
hLg=legend(num2str([2;5;10;20;25]), ...
'orientation','horizontal', ...
'location','north'); legend('boxoff')
yields
With more recent release than I have here (R2014b) you can also move the y-axis to the origin with
hAx.XAxisLocation='origin';
to essentially duplicate the example.
would for example color the N th bar in the group red if don't like default colors (which, granted, aren't the most pleasing they could be).
ADDENDUM: OK, am back so to do error bars on the bar plot--
Start by combining the data into an array instead of using separate variables -- a general coding principle in Matlab, btw, makes things much more efficient as a rule.
ABCerr=[A_error;B_error;C_error];
x=repmat([1:size(ABC,1)].',1,size(ABC,2));
xOff=bsxfun(@plus,x,[hBar.XOffset]);
hold all
hEB=errorbar(xOff,ABC,ABCerr,'.');
Above will have a set of default colors for the errorbar series that won't necessarily coincide with the bar series already have.
I do not know where the default bar color series values are stored; the default 'FaceColor' value is the string 'Flat' which isn't of much help in matching to but as noted above you can reset it to either one of the eight defined color strings or any RGB triplet to match up with the default errorbar colors could be
for i=1:length(hBar)
hBar(i).FaceColor=hEB(i).Color;
end
or choose a set for both. Or, on further reflection and looking at the plot created, maybe 'k' for all the error bars might help them be visible, particularly the lower limits that are in the bars.
Another fixup would be
hBar(1).BaseLine.Color='k';
to show the baseline under the bars; it follows the y-axis color by default so became invisible when turned that color to match the figure background.
Best Answer