File Exchange

image thumbnail


version (6.05 KB) by Pierre Mégevand
ERRORBAR_GROUPS produces customizable grouped bar plots with overlaid error bars.


Updated 30 Sep 2017

From GitHub

View Version History

View license on GitHub

At its most basic, this function produces bar plots similar to those obtained using MATLAB's BAR(Y,'grouped') function call, and then overlays error bars onto the corresponding bars.
ERRORBAR_GROUPS allows customizing the plot in several ways. For instance, both the width of the bars themselves and that of the error bars can be adjusted. The function allows asymmetric values for the lower and upper bounds of the error bars. The colors of the bars and error bars can also be customized. ERRORBAR_GROUPS allows transmitting optional input property-value pairs to both the BAR and ERRORBAR functions, making it quite versatile.

Cite As

Pierre Mégevand (2021). pierremegevand/errorbar_groups (, GitHub. Retrieved .

Comments and Ratings (50)

Filippo Cerpelloni

Pierre Mégevand

Hi Wei-Li, sorry to read that. I just checked on my machine and example 4 in the help section worked fine. Does the error message include more information, e.g. line number? Could you try and run the example code to see if that works? Thanks.

Wei-Li Hsu

the 'optional_bar_arguments' and 'optional_errorbar_arguments' do not work for me. I got an error message as "Unbalanced or unexpected parenthesis or bracket." Please help. Thanks.

Emanuel Ebner

Tawfik Al-Nusairi

Donghoon Yeo

I found the same problem as Pierre... And Kyle and Xi were correct about how to fix the problem. Much thanks:)

Prateek Gupta

Markus Wirnsberger

Jose Pardo

Xi Zong

I think Kyle was right. I simply commented out 250 - 253 and the issues on the error bar x position were gone.

Kyle Hansen

I figured out where this error is occurring. What is it you are doing on lines 250-258? What seems to occur is that:
Changes the x position of the error bars to be where the 1st error bar is, shifted by half the bar width in the 1st position. That is what the code says to do, so I'm wondering why you are doing that.
The same things happen with the 4th & 5th errorbar, but the 6th is fine. It then seems to happen again for the 7th and 8th, though I'm not plotting those.

If you can explain what you were trying to do with those versions of code, I'm happy to push changes to the Git. I'm using Matlab 9.1

Pierre Mégevand

Hi Javier, as long as I don't have access to another version of MATLAB than R2009b, I can't work on making the code compatible with the most recent releases. I am still hoping to get my hands on a newer version soon. Stay tuned.

I am using R2016b and I find the same issue as Sankar... more than 3 groups lead to bars being misplaced. Any solutions for this?

Pierre Mégevand

Hi Sankar, with my version of MATLAB (R2009b), I have no difficulty plotting more than 3 groups (e.g. the second example in the help section of the function works just fine on my machine). Which version are you using?

sankar basu

Hi ! It does not seem to work properly with more than three groups. The error-bars for the forth group gets misplaced. There appears to be missing a coordinate transformation.

Pierre Mégevand

Dear Ajay, MATLAB's BAR function does not have an option to fill the bars with hatches or other patterns, and ERRORBAR_GROUPS does not provide such functionality either. You could use other user-contributed functions available on the File Exchange (see a sample here:, or else create the bar plot with plain bars using ERRORBAR_GROUPS and then edit it in a vector drawing program like Inkscape, for instance. Hope this helps.

Ajay Goyal

Thank you Mr. Pierre. I used your code which works very well. I need to generate similar graph but with hatched bars (as in your case colored bars are generated). Can you please help.

Lorena Freitas

shaoqing xu

TK1234 help me a lot, the web is useful

Nolwenn Lévêque

Thank you !!!

Rachael Elward

I had the same problem as Rashdj with the error bars being misplaced when the number of groups exceeds 3. I commented out the lines that Rashdj suggested and the code worked perfectly. Thank you! :)

Jae Young


wx l

I find that 'xdata' was changed in ver. 2016, and a solution is using 'h.Bar.VertexData(1,:)' instead (By Matt fileexchange/22826-adjust-error-bar-width)


instead of using "distinguishable_colors" better define your own colors and modify line 125.

the rest of the code looks great.

yu zhang

Easy to use, thanks

psan da

Does not work with 2012b:
Undefined function 'makecform' for input arguments of type

Error in distinguishable_colors (line 93)
C = makecform('srgb2lab');

Error in errorbar_groups (line 125)
bar_colors=distinguishable_colors(N_grps); % get some
distinguishable colors for the bars!

Kicking off the dependency on distinguishable_colors helps. I understand the point of using that dependency but just using builtin colormap as jet or hsv(N_grps) would make this package be more robust.

Michele Guerreri

Ok...I've just read below. Thank you anyway!

Michele Guerreri


I'm trying to subplot figures obtained by using the errorbar_groups function, but I can't.

Do you have some hint on how to do that?

Pierre Mégevand

Hi Marco, thanks for the positive feedback. To use the function with subplots, you should use the 'FigID' and 'AxID' optional inputs. Here is an example:


Let me know if it helps.

Marco Bertleff

Hey, thanks for this really helpful code!
Somehow it does not work in subplots for me - it only produces empty subfigures and displays the last subfigure in a separate figure. If there is a way to fix this I would be happy to know!



Thank you for the great function. I had the exact same issue as Rashdj, and his solution worked for me. No apparent adverse consequences of commenting out that block. I am using (R2015a)

Hongyan Zhao

Pierre Mégevand

Dear Rita, thanks for your interest. DISTINGUISHABLE_COLORS is a MATLAB File Exchange function by Timothy E. Holy that is available here ( and is also provided in the zip file together with ERRORBAR_GROUPS.

It is not necessary to have the Image Processing Toolbox (or any toolbox) for ERRORBAR_GROUPS to run. The only drawback right now is that it runs properly only on MATLAB versions older than R2014b.


How to avoid of using the function DISTINGUISHABLE_COLORS?Since I don't have image processing toolbox.
Can I use this Grouped bar function without having Image processing toolbox?

Pierre Mégevand

Dear Rashdj, thanks for the feedback. I've been unable to reproduce the issue on my version of MATLAB (R2009b), so I suspect that the problem stems from the change in graphic object handling in more recent versions. I am reluctant to edit the code without being able to check myself what the consequences are, but once I can get my hands on an updated version of MATLAB (hopefully in the coming weeks) I'll debug this issue. Thank you again for the feedback.


Thanks for this useful function.

There is a problem with the lines that ankara base mentions, which results in misplaced error bar whenever the number of groups exceeds 3.

Line 254-257:


Making the change that he suggests did not solve the problem for me.

But commenting the whole "for" block where it stand, did solve the issue. I am using Matlab 2015b.

Can you please fix and redistribute.

Pierre Mégevand

Dear Sankar, thanks for your message and sorry for the long response latency.

I have tried to reproduce the issue using the following 3 examples:




In all 3 cases, I obtained "normal-looking" plots where both the bars and the error bars would be positioned as expected. I am using version R2009b of MATLAB.

Could it be that the issue you experienced is specific to MATLAB versions R2014b and above? errorbar_groups is unfortunately not yet compatible with these most recent versions of MATLAB. Stay tuned, though, for future updates to the function.

wei li

sankar basu

There is a BUG in the code. (Line 254-257)

In the following block, 9 should be replaced by N_bars where


Otherwise the script would only run for <= 9 column bar_input and errorbar_input arrays

Please fix and redistribute

Pierre Mégevand

Yes TK1234, MATLAB changed the way it handles graphic objects on version R2014b. Therefore, versions of MATLAB from then on should unfortunately not be expected to run errorbar_groups smoothly. As soon as I can get an updated version of MATLAB, I'll debug this issue.


Problem even on R2014b:

Try this:

"errorbar_groups([264 389 375 400 ;461 429 469 400],[5 10 10 8 ;4 8 16 8],'bar_names',{'Sandblasted','Ground Parallel','Ground Perpendicular','Ground Transverse'});"

When you add a fourth group, the error bars are displaced. Please help fix!

Pierre Mégevand

Sorry about that Thomaz. Unfortunately, since I don't have R2015a I can't debug this issue adequately. Stay tuned though, when I upgrade my matlab version i'll definitely update the code to errorbar_groups.

Thomaz Botelho

The error bars are misplaced again in 2015a :(

Pierre Mégevand

Thanks for the feedback Liviu. Unfortunately, I do not have R2015a and am therefore unable to debug the graphical display problems that cropped up in the function following the R2014b update and its overhaul of graphical objects. IF you find a way to deal with the issue on your side, please keep me posted and I'll happily update the code.


Hi Pierre. Let me first thank you for this function!
I'm dealing with this problem. I'm trying to plot in 2015a a single group with some bars in it, e.g.:
Y = randi(10,[1 7]);
e = randi(3,[1,7]);
Bars are misplaced. If I transpose instead Y and e: errorbar_groups(Y',e')
it produces groups that are too far apart
Could you suggest a way to fix this?

Pierre Mégevand

Thanks for the feedback Tyler. Have you tried downloading the latest version of errorbar_groups (updated on Apr 8, 2015)? The code fix courtesy of Ariel Tankus might solve the issue. Thanks for keeping me posted.

Tyler Bleazard

This does not work for 2014b, the error bars are misplaced. Could you please fix

Pierre Mégevand

Thanks for your message Jonas. Would you mind being more specific as to what did not work? Perhaps with the error message thrown my MATLAB or the code line numbers that are at fault?

I just re-ran all 4 examples from the function and I did find out a problem when plotting different lengths for the upper and lower error bars. I've corrected the problem and re-uploaded the function. But I have the feeling that you were pointing out to another issue.


Dear Pierre,

thanks for your nice grouped bar plot with error bar function.

In my case the simple example does not work. I think this is due to a mistake in the part of the code where you set the error bar width. This can be solved by removing this part, and adding the following argument when you call the errorbar function:
'LineWidth', errorbar_width, ...

Hope this helps.

MATLAB Release Compatibility
Created with R2009b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!