version 1.7.0.0 (5.86 KB) by
Pierre Mégevand

ERRORBAR_GROUPS produces customizable grouped bar plots with overlaid error bars.

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.

Pierre Mégevand (2021). pierremegevand/errorbar_groups (https://github.com/pierremegevand/errorbar_groups), GitHub. Retrieved .

Created with
R2009b

Compatible with any release

**Inspired by:**
barweb (BARgraph With Error Bars), Adjust error bar width, Create Healthy Looking Error Bars

**Inspired:**
superbar

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

Start Hunting!Create scripts with code, output, and formatted text in a single executable document.

Filippo CerpelloniPierre MégevandHi 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 Hsuthe '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 EbnerTawfik Al-NusairiDonghoon YeoI found the same problem as Pierre... And Kyle and Xi were correct about how to fix the problem. Much thanks:)

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

Kyle HansenI figured out where this error is occurring. What is it you are doing on lines 250-258? What seems to occur is that:

he_xdata(4:9:end)=he_xdata(1:9:end)-errorbar_width*bar_width/2;

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égevandHi 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.

Javier Gonzalez CastilloI 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égevandHi 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 basuHi ! 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égevandDear 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: https://blogs.mathworks.com/pick/2011/07/15/creating-hatched-patches/), 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 GoyalThank 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 Freitasshaoqing xuTK1234 help me a lot, the web is useful

Nolwenn LévêqueThank you !!!

Rachael ElwardI 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 Younggoood

wx lI 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)

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

the rest of the code looks great.

yu zhangEasy to use, thanks

psan daDoes not work with 2012b:

Undefined function 'makecform' for input arguments of type

'char'.

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 GuerreriOk...I've just read below. Thank you anyway!

Michele GuerreriHi,

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?

Thanks,

Michele.

Pierre MégevandHi 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:

hf=figure;

ha1=subplot(2,1,1);

ha2=subplot(2,1,2);

bar_input=rand(3,8)/2+0.5;

errorbar_input=rand(3,8)/8;

errorbar_groups(bar_input,errorbar_input,'FigID',hf,'AxID',ha1);

bar_input=rand(4,6)/2+0.5;

errorbar_lower=zeros(size(bar_input));

errorbar_upper=rand(4,6)/8;

errorbar_groups(bar_input,errorbar_lower,errorbar_upper,'FigID',hf,'AxID',ha2);

Let me know if it helps.

Marco BertleffHey, 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!

JonathanHi

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 8.5.0.197613 (R2015a)

Hongyan ZhaoPierre MégevandDear Rita, thanks for your interest. DISTINGUISHABLE_COLORS is a MATLAB File Exchange function by Timothy E. Holy that is available here (http://www.mathworks.com/matlabcentral/fileexchange/29702) 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.

RitaHow 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?

Thanks

Pierre MégevandDear 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.

RashdjThanks 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:

he_xdata(4:9:end)=he_xdata(1:9:end)-errorbar_width*bar_width/2;

he_xdata(7:9:end)=he_xdata(1:9:end)-errorbar_width*bar_width/2;

he_xdata(5:9:end)=he_xdata(1:9:end)+errorbar_width*bar_width/2;

he_xdata(8:9:end)=he_xdata(1:9:end)+errorbar_width*bar_width/2;

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égevandDear Sankar, thanks for your message and sorry for the long response latency.

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

bar_input=rand(3,11)/2+0.5;

errorbar_input=rand(3,11)/8;

errorbar_groups(bar_input,errorbar_input);

bar_input=rand(11,3)/2+0.5;

errorbar_input=rand(11,3)/8;

errorbar_groups(bar_input,errorbar_input);

bar_input=rand(11,11)/2+0.5;

errorbar_input=rand(11,11)/8;

errorbar_groups(bar_input,errorbar_input);

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 lisankar basuThere is a BUG in the code. (Line 254-257)

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

he_xdata(4:9:end)=he_xdata(1:9:end)-errorbar_width*bar_width/2;

he_xdata(7:9:end)=he_xdata(1:9:end)-errorbar_width*bar_width/2;

he_xdata(5:9:end)=he_xdata(1:9:end)+errorbar_width*bar_width/2;

he_xdata(8:9:end)=he_xdata(1:9:end)+errorbar_width*bar_width/2;

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

Please fix and redistribute

Pierre MégevandYes 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.

TK1234This works better:

http://www.mathworks.com/matlabcentral/answers/102220-how-do-i-place-errorbars-on-my-grouped-bar-graph-using-function-errorbar-in-matlab-7-13-r2011b

TK1234Problem 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égevandSorry 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 BotelhoThe error bars are misplaced again in 2015a :(

Pierre MégevandThanks 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.

LiviuHi 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]);

errorbar_groups(Y,e)

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égevandThanks 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 BleazardThis does not work for 2014b, the error bars are misplaced. Could you please fix

Pierre MégevandThanks 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.

JonasDear 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.