File Exchange

image thumbnail

pierremegevand/erro​rbar_groups

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

47 Downloads

Updated 30 Sep 2017

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.

Comments and Ratings (47)

Emanuel Ebner

Donghoon Yeo

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

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

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?

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.

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

shaoqing xu

TK1234 help me a lot, the web is useful

Thank you !!!

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

goood

wx l

wx l (view profile)

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)

steven

steven (view profile)

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

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

Hi,

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.

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:

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.

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!

Jonathan

Hi

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 Zhao

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

Rita

Rita (view profile)

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

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.

Rashdj

Rashdj (view profile)

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:

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.

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:

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 li

wei li (view profile)

sankar basu

There 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

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.

TK1234

TK1234 (view profile)

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!

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.

The error bars are misplaced again in 2015a :(

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.

Liviu

Liviu (view profile)

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

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.

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

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.

Jonas

Jonas (view profile)

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.

Updates

1.7.0.0

Removed dependency on DISTINGUISHABLE_COLORS.

1.6.0.0

Thanks to Ariel Tankus for fixing an issue with MATLAB version R2014b (and presumably beyond) where the errorbar width could no longer be customized. This is apparently due to a change in the way MATLAB handles error bars in the graphics.

1.5.0.0

Corrected an issue where no error bar was plotted in the case that one side (upper or lower) was set to 0. See also comments.

1.4.0.0

Simplified the code defining default values and dealing with optional input arguments. Added option to skip plotting the error bars altogether.

1.3.0.0

With help from David Groppe, added optional handles inputs for plotting into existing figure or axes.

1.2.0.0

(this time with the correct files in the archive...)

1.1.0.0

Added a line at the end to adjust the limits of the X axis to the plot.

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

Discover Live Editor

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


Learn About Live Editor