File Exchange

image thumbnail

Create Healthy Looking Error Bars

version 1.3 (4.16 KB) by

Plots nice error bars as the default. Can also top group plots with error bars.



View License

% Description errorb(x,y,varargin)
errorb(Y,E) plots Y and draws an error bar at each element of Y. The error bar is a distance of E(i) above and below the curve so that each bar is symmetric and 2*E(i) long. If Y and E are a matrices, errob groups the bars produced by the elements
in each row and plots the error bars in their appropriate place above the bars.

errorb(X,Y,E) plots Y versus X with symmetric error bars 2*E(i) long. X, Y, E must be the same size. When they are vectors, each error bar is a distance of E(i) above and below the point defined by (X(i),Y(i)).

errorb(X,Y,'Parameter','Value',...) see below

% Optional Parameters
horizontal: will plot the error bars horizontally rather than vertically
top: plot only the top half of the error bars (or right half for horizontal)
barwidth: the width of the little hats on the bars (default scales with the data!) barwidth is a scale factor not an absolute value.
linewidth: the width of the lines the bars are made of (default is 2)
points: will plot the points as well, in the same colors.
color: specify a particular color for all the bars to be (default is black, this can be anything like 'blue' or [.5 .5 .5])
multicolor: will plot all the bars a different color (thanks to my linespecer function)
colormap: in the case that multicolor is specified, one
may also specify a particular colormap to
choose the colors from.
% Examples
y=rand(1,5)+1; e=rand(1,5)/4;
hold off; bar(y,'facecolor',[.8 .8 .8]); hold on;

defining x and y
x=linspace(0,2*pi,8); y=sin(x); e=rand(1,8)/4;
hold off; plot(x,y,'k','linewidth',2); hold on;

group plot:
values=abs(rand(2,3))+1; errors=rand(2,3)/1.5+0;

% motivation for the function
It is possible to plot nice error bars on top of a bar plot with Matlab's built in error bar function by setting tons of different parameters to be various things.
This function plots what I would consider to be nice error bars as the default, with no modifications necessary.
It also plots, only the error bars, and in black. There are some very useful abilities that this function has over the matlab one.

Comments and Ratings (11)


Elisa (view profile)

errorb won't work on R2015. The solution is the same as for a similar function :

Would be good to update the code? Nice function otherwise, I've been using it a lot.


Katie (view profile)


Aakash (view profile)

How about being able to set transparency of the error bars? That way when I plot horizontal errorbars I can set linewidth to 50 for example, and make the error bar stretch across the whole height, set transparency to 0.5 (so I can still see my data bars).

This is useful for showing std() fro


Aakash (view profile)

Really nice job!
Now I don't know where to read a changelog or anything but I had this problem that the legend wouldn't show the right colors for the line plots overlaid on the bar plot.

I just updated my errorb.m from here and it works now! Great job keeping this up to date.

Thank you.

Thank you Math Moose! I have implemented your suggestion.


Thanks, works nice, except that it adds legend entries for all the error bar lines. You should set
for all of the error bar plot handles.

Also, you should change the "All rights reserved" in the errorb.m comments to the BSD license.

Andrew Bogaard

Thanks for providing this program. This is very useful. It helped me a lot to represent my data

It should work fine for a logarithmic vertical axis.


Oscar (view profile)

The error bars over the bar graphs are indeed simple to create and nice for publications. Would you consider fixing Matlab's bar function to allow a logarithmic vertical axis?

Rob Campbell

Rob Campbell (view profile)

Often bar charts hide useful information. The option to overlay the jittered raw data over each bar would be useful.



I implemented the suggestions of the most eminent Math Moose.


Changed the screenshot


The linspecer function is now included.

MATLAB Release
MATLAB 7.8 (R2009a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video