MATLAB Answers

How to change the color for bar graphs and create space between grouped bars?

53 views (last 30 days)
R7 DR on 8 Jul 2017
Edited: Hunter Pruett on 23 May 2020
Hi How to change the face color of the individual bar in a grouped bar chart. How to create space between different groups in a bar chart.
For example, as shown in the below image. I have 3 different category i.e. A,B,C and each category has 5 bars i.e. 2,5,10 20 and 25. How to create this kind of graph in matlab.
The data for A, B, C are... A= [10,13,9,6,20]' B= [2, 4, 7, 9,10]' C=[-2,-3,-8,-10,-11]'


Sign in to comment.

Accepted Answer

dpb on 8 Jul 2017
Edited: dpb on 8 Jul 2017
ABC=[ [10,13,9,6,20]' [2, 4, 7, 9,10]' [-2,-3,-8,-10,-11]'].';
hBar=bar(ABC,'grouped'); % basic grouped bar plot, keep handle
hFg=gcf; hAx=gca; % handles to current figure, axes
ylim([-20 30]) % space out a little extra room
hAx.XTickLabel={'A';'B';'C'}; % label x axis
hAx.YColor=get(hFg,'color'); box off % hide y axis/labels, outline
hLg=legend(num2str([2;5;10;20;25]), ...
'orientation','horizontal', ...
'location','north'); legend('boxoff') % add legend
With more recent release than I have here (R2014b) you can also move the y-axis to the origin with
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.
x=repmat([1:size(ABC,1)].',1,size(ABC,2)); % bar x nominals
xOff=bsxfun(@plus,x,[hBar.XOffset]); % bar midpoints x
hold all % keep axis limits, etc.
hEB=errorbar(xOff,ABC,ABCerr,'.'); % add errorbars
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)
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
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.


Show 1 older comment
dpb on 8 Jul 2017
"... Is it possible to keep the 'C' inside the bar chart?"
I thought it was perfect! {VBG} :)
Not with the axis object, no. You'll note your original is the same way. You could use text and write the axis labels where you want them.
You can overlay an errorbar plot on top of bar but it takes a little more than I've got time to do right now.
I think there's a FEX submittal but not positive.
As hint, there's a hidden property .XOffset in the bar handle properties that gives the offset from the nominal x to the center of each bar. Retrieve and use that to set the error bar positions if use errorbar
I'd give example but I'm already late for an appointment in town...
dpb on 15 Sep 2017
Again move Answer to Comment...dpb
How to turn those vertical bar graphs into horizontal bars keeping the same structure (same code as your first comment.
dpb on 15 Sep 2017
For horizontal bar graph, use barh
Prior to release after R2014b (but undocumented as to when except perhaps in individual release notes) errorbar only supported vertical error bars; at some point w/ HG2 it added a new 'orientation' property.
Same ideas hold as before; simply swap the axes looking at for the specific properties as needed...

Sign in to comment.

More Answers (2)

R7 DR on 9 Jul 2017
Hi Thanks. Now its perfect :) :)
I still have one question. I am trying to give hatching lines in place of colors using the following file, but the quality of the image is very bad.
Could you please tell me how to give differnt hatching pattern on bar chart?

  1 Comment

dpb on 9 Jul 2017
Another Matlab weakpoint -- there's no explaining TMW's failure to include multitude of hatching patterns in lo! these many years.
I've also tried a number of FEX hatching submissions and had only marginal success with any of them, sorry.
The ones I've used worked (to a lesser or greater degree of pleasing success) directly on patch object handles; I don't know about trying them with bar series objects.
In short, I don't know enough specifically to help; I'd suggest it's a big enough oversight to be worth a bug/feature report and enhancement request. I don't recall if I actually submitted one back when was playing with those routines or not; that was a couple years ago now...

Sign in to comment.

Hunter Pruett
Hunter Pruett on 23 May 2020
Edited: Hunter Pruett on 23 May 2020
Just finished the barmod function--controls relative position of just about everything in the plot. Hope this helps anyone else with this question--I was having similar issues and found the lack of functionality a bit disappointing. See the demo for color modification and such.


Sign in to comment.


Translated by