version 1.2.0.0 (2.78 KB) by
Bolu Ajiboye

Plots a fully customizable grouped bar graph with error bars

barweb.m calls the BAR and ERRORBAR MATLAB functions to produce bar graphs with error bars. The resultant graph is fully customizable, from group naming to legend inclusion to colormap setting. It assumes an m-by-n y-input matrix and plots m groups of n bars. The errorbar matrix must be setup the same way as the barvalues matrix (i.e. same dimensionality, matched values must have same indexes)

Syntax:

barweb(barvalues, errors, width, groupnames, bw_title, bw_xlabel, bw_ylabel, bw_colormap, gridstatus, bw_legend)

Bolu Ajiboye (2021). barweb (BARgraph With Error Bars) (https://www.mathworks.com/matlabcentral/fileexchange/10803-barweb-bargraph-with-error-bars), MATLAB Central File Exchange. Retrieved .

Created with
R14SP3

Compatible with any release

**Inspired:**
superbar, BarTwoYaxis, Create Healthy Looking Error Bars, barwebpairs (pair bars within groups and between groups), pierremegevand/errorbar_groups

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.

Jun LiuNot very robust at all. Line 151 failed for no reason.

Jun LiuWhat's the 'children' in the get lines from line 151?

zhang zipengCalvin KohMay I know how can you show the legend of the errorbar plot rather than the legend of the bar plot?

Suhas Chelian+1 for Fan Zhang's comment on 29 Oct 2017; thank you!

JackyKatrin DätwylerWhen I plot a barplot with error_side = 1 (one sided error bars) the bars inside each Group get rearranged. So the value represented by the first bar when plotting with error_side = [] is the last bar when plotting with error_side = 1. This would be no big Problem, but the error bars seem not to be rearranged. Therefore the first value/bar gets the error bar from the last value/bar and so on. Do you know any solution to that?

And is it somehow possible to also plot negative values?

jun fanMingzhu TangFan ZhangFor the error "Index exceeds matrix dimensions. Error in barweb (line 152) x = mean(x([1 3],:)); "

Replace Lines 151 and 152 with following would work. It worked well for me using Matlab 2015a.

% x =get(get(handles.bars(i),'children'), 'xdata');

% x = mean(x([1 3],:));

x = handles.bars(i).XData + handles.bars(i).XOffset;

William KenkelI am also getting the error "Index exceeds matrix dimensions. Error in barweb (line 152) x = mean(x([1 3],:)); " using identically sized data and error matrices

Jacob PopeGreat code, but is there anyway to change the code so that asymmetric error bars are used?

AAA BBBJasper CoolsThanks!

handecAmin MahmoodiExcellent! Very useful app.

Evyatar CohenHi all,

Dose anyone know why the one side error option doesn't work? It show only a line Parallel to the column. Also it is not in the right value (that the std spouse to be).

jfcabanaHi, that function seems useful! I get an error though trying to use it. My data and error matrices are both 9 x 2. I get this error :

Index exceeds matrix dimensions.

Error in barweb (line 152)

x = mean(x([1 3],:));

Evyatar CohenHi all

How can i plot multi groups on one graph, like in the example image?

Evyatar CohenHi all

First this function is very useful.

How can i plot multi groups on one graph, like in the example image?

imu931Very useful!

bugguts99Hi there,

I have stipulated to plot only one error bar (+ve) and it does this; however, when I save it as .fig, .eps or .png the image plots with 2 error bars (+ve & -ve). I have deleted and re-drawn the plots a million times, to different formats and keep having the same problem. Am I missing something?

Kiran Karrasara shaheenThis is a very nice function, and I want something exactly like figure 1, though I seem unavailable to get my legends on each bar to work. I'm new in Matlab and if someone could help me how the inputs are given to the function barweb to achieve figure 1 It would be a great help. Thanks in advance.

DavidWorks great with the change Flo provided. Thanks.

FloTo get the code back working with R2015a (8.5.0.197613), the plot errors loop has to be changed to:

for i = 1:numbars

x = bsxfun(@plus, handles.bars(i).XData, [handles.bars(i).XOffset]');

handles.errors(i) = errorbar(x, barvalues(:,i), errors(:,i), 'k', 'linestyle', 'none', 'linewidth', 1);

ymax = max([ymax; barvalues(:,i)+errors(:,i)]);

end

FlocmoOleg KomarovSince R2012b you can use errorbar:

x = pi/10:2*pi;

y = sin(x);

e = std(y)*ones(size(x));

bar(x,y)

hold on

errorbar(x,y,e,'rx')

RainerOh no, doesnt work with 2014b anymore.

Greg@Jaroslav Hlinka. Fixed this bug. Changed the plot errors loop to this. Set 'error_sides' to 3.

% Plot erros

for i = 1:numbars

x = get(get(handles.bars(i),'children'), 'xdata');

x = mean(x([1 3],:));

if error_sides == 3

pValues = barvalues;

pValues(pValues<0)=NaN;

nValues = barvalues;

nValues(nValues>0)=NaN;

handles.posErrors(i) = errorbar(x, pValues(:,i), zeros(size(errors(:,i))), errors(:,i), 'k', 'linestyle', 'none', 'linewidth', 2);

handles.negErrors(i) = errorbar(x, nValues(:,i), errors(:,i), zeros(size(errors(:,i))), 'k', 'linestyle', 'none', 'linewidth', 2);

else

handles.errors(i) = errorbar(x, barvalues(:,i), errors(:,i), 'k', 'linestyle', 'none', 'linewidth', 2);

end

ymax = max([ymax; barvalues(:,i)+errors(:,i)]);

end

Aaron PancostLiIt is working，thank you very much！

Rakesh ChalasaniShuvraAwesome

MichaelWonderful function, very useful. Just a quick question, my error value of choice at the moment is SEM; when I input my SEM matrix (1 standard error of the mean) as my "errors" values, does this function plot 1 SEM above and below the mean (showing 2 SEM in total), or does it split 1 SEM in half and shows the upper and lower half, above and below the mean respectively (showing 1 SEM in total) ?

Thanks a bunch for the feedback!

M

ShatrughanHey Plamen..what is your data..I may help you as have been using this great script since long for plotting..let me know about your data..

PlamenHow do you group up different data on one plot? In the example image there are 3 groups with 'type1 type2 type3 type4' but I cannot figure out how.

Nolan Conawaygreat!

DianeThis is a really great program. One additional feature that would be great is making it so you can make a break in the y-axis (e.g. if you have a break from 100 to 1000) when plotting data of different magnitudes.

johnWorks well.Great

George McFaddenI have the same problem as the previous two comments here. The groupnames input only displays 1 string on the x axis, and I have a 6x1 cell variable = groupnames. Appears to be a common problem with other users. Please Advise how to fix!

Thanks.

G

PShow to plot with secondary axis means values of two bars on two different y axis

DavidI seem to have run into a bug. I want to have a single bar for several groups and I set the groupnames variable to the names I want. But I end up with one group and multiple bars. I've tried inputing an mx1 vector and 1xm vector to see if that's the problem but both times I get the same results. Commenting out lines 120 and 121 fixed it. Any ideas?

Honggood job

MarianaYi YINamit Singhgood

dani dinArturo Moncada-TorresNice file. I recommend you to look at this post (http://blogs.mathworks.com/loren/2009/05/05/nice-way-to-set-function-defaults/) regarding variable inputs for functions.

Keep up the good work ;-)

musi(The above statement is to put samples values associated with each bar in the x-axis)

musiPretty great:

it is working

% groupnames

{'1000', '2000', '5000', '10000', '15000', '20000', '25730'},

PearlThank you!

Arturo Moncada-TorresRachelwhat are the options to set the gridstatus input to?

Casey TaThanks, this was very helpful!

Casey TaKarenIt would be nice if we could set different upper and lower error values... Otherwise quite cool

Ali AliThanks a lot

Juan CarlosThanks!

Erinto plot bars horizontal

view(90, -90)

or view(-90, 90)

Patrick A.C GGreat. Thanks!

Jeff ChenWorks GREAT!

RossThis is really nice. If using values in only one group (monthly rain data, for example), is there any way to add labels to the x-axis?

JamesThanks Bolu! This is what I was looking for.

PearlJesse BrownHandy function. One problem: when I save a figure with one-sided bars to .png or .tif, two-sides bars show up.

Debanjan GoswamiReally nice and easy. But I have got a problem. I can't plot the 'X-tick' like type1, type2... written in the example figure. I have the text for each bar in a separate cell array. How to incorporate?

Thanks

Ankereally helpfull!

Luis Felipe GiraldoNice!

Dung ChuGreat. I just need view(90, -90) after all to have horizontally-aligned bar charts.

ArthurRoman BalabinJaroslav HlinkaThanks for the function! Useful, although I think I may have found an annoying bug: using 'error_sides = 1', when I save tiff file, it has the errorbar all the way down to x-axis (it is still ok when the initial figure is created). Using MATLAB R2007a on windowsXP. Any idea why/Any quick fix available?

PS. btw, the usage in the .m file might not be up to date - e.g. mentioning "barwebtype" parameter which is not amongst the function parameters.

Greg ImamuraGreat program- I modified mine to allow for lower and upper errors

MatthewFélix ChénierClean and simple, it does a very good job. Thank you

WenjiaExcellent! That is just what I need.

MastanehKshitiz GuptaThanks. It is very useful.

René QuilodranThanks Bolu !!!!! you did a great job!!!

Nikolay StoykovGreat job, Bolu! Thanks.

Lan Huong Vu ThiThanks. It is useful for me !

Brajendra SinghGreat!

Jorge TGreat! saved me a lot of time!

A VanderWystBen EngelThis was a great help - thanks!

alyssa smithyou're my hero. this just saved me a mountain of trouble!!

Y GuoThanks! Good job! Matlab should consider incorporating this!

Amanda WhitmireGreat - exactly what I needed. Thanks for sharing it.

H Hunggreat stuff.. any chance of updating it so that the errors can have an upper and lower bound like the matlab errorbar function?

Kim BostroemCoolomat!

Why hasn't the great Matlab thought of this himself? It's SO useful!

Juan HincapieExactly what I needed!

Andy CJust what I always wanted.

Dimka PVery helpful! Thanks!

John ElveThe script itself is good but it lacks the possibility of a variable number of input arguments!

J XThis is very helpful, which helped me draw the graphs in a few secs.

Diana BedollaFor the plotting it works really good, but when I introduce the groupnames it repeats the same

first groupname in each x-tick. Any idea what could be the problem?? Thanks for the file!!

Steve FlemingOne of the very few oversights in the graphics toolbox is not having an easy way of plotting error bars! This is great and saved me a lot of time.

germana bathank you so much!

Richard LemonThis works very well. What a big help. Thanks!

Will HuntingThanks a lot !

James BlakeWorks well with R2006a. Nice and easy to change the colours of the bars and such.

Richard CuiNeat job!

Michael FrankNice! produces very pretty graphs, generally very easy to use.

Scott GSmall problem with group labelling in R14.

Fix on line 124:

Add additional arguments to set(...)

set(gca, ..., 'XTick', 1:numgroups);

Angela ChapmanUnfortunately for me, not compatible with Matlab 6.0.

Jiro DokePretty good, but it doesn't leave the user much control to make modifications. You should at least return the handles of the bar and errorbar objects, so that the user can modify their properties. Along the same line, you should also return the handle of the legend axes. I prefer leaving the legend (as well as title and labels) out for the user to do on their own, since it allows them to customize properties like position, fontsize, fontweight, etc.

Shai Shen-Orr