Code covered by the BSD License  

Highlights from


5.0 | 17 ratings Rate this file 285 Downloads (last 30 days) File Size: 2.75 KB File ID: #26311
image thumbnail




07 Jan 2010 (Updated )

Creates an attractive shaded error region rather than discrete bars.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information

Creates a continuous shaded error region rather than discrete bars. Error region can either be specified explicitly or calculated on the fly based upon function handles. Handles of the plot objects are returned in a convenient structure. Please use plot2svg (FEX submission 7401) if you wish to get high quality printed files with the OpenGL renderer enabled. You will need OpenGL to get transparency to work.

function H=shadedErrorBar(x,y,errBar,lineProps,transparent)
  Makes a 2-d line plot with a pretty shaded error bar made
  using patch. Error bar color is chosen automatically.
  x - vector of x values
  y - vector of y values or a matrix of n observations by m cases
      where m has length(x);
  errBar - if a vector we draw symmetric errorbars. If it has a
           size of [2,length(x)] then we draw asymmetric error bars
           with row 1 being the upper bar and row 2 being the lower
           bar. ** alternatively ** errBar can be a cellArray of
           two function handles. The first defines which statistic
           the line should be and the second defines the error
  lineProps - [optional,'-k' by default] defines the properties of
              the data line. e.g.:
              'or-', or {'-or','markerfacecolor',[1,0.2,0.2]}
  transparent - [optional, 0 by default] if ==1 the shaded error
                bar is made transparent, which forces the renderer
                to be openGl. This means that saved .eps files
                won't look so good because they'll be rasters not

  H - a structure of handles to the generated plot objects.


This file inspired Mseb(X,Y,Err Bar,Line Props,Transparent), Shaded Error Bar Yy, Mean Square Displacement Analysis Of Particles Trajectories, and Boundedline.M.

MATLAB release MATLAB 7.8 (R2009a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (23)
11 Dec 2014 Nicolas  
05 Dec 2014 Michael

Super awesome. Easy to use. Beautiful graphics.

29 Sep 2014 Andreas Trier Poulsen  
26 Sep 2014 Tihana

very helpful! Thank you.

25 Apr 2014 Irene Seco  
05 Mar 2014 Alex  
20 Feb 2014 Rob Campbell


y=randn(30,80)+10; x=1:size(y,2);

19 Feb 2014 Constantinos Constantinos

Thanks for this code, it is a very nice way to represent standard errors. I just have an issue I can't solve: In one of my graphs, I would like to use a logarithmic scale for the x axis.. as soon as i change it from linear to log on the figure manager (or if i use set(gca,'xscale','log')), the axis change to log but the graph remains the same.. I have the feeling the function is not compatible to log graphs. do you have any idea how i could solve this problem? i tried to replace all "plot()" in your code with "semilogx()" but it also did not work.

Thanks in advance

13 Jan 2014 Rob Campbell

Bernadette, set(gcf,'Renderer','Painters')

13 Jan 2014 Bernadette

thanks for this nice code, unfortuntately i can't figure out this openGL thing.. you wrote that if it's saved as.eps then it will look like a raster, and that's what it does in my plot but I don't know how to change it?! can you help?

12 Dec 2013 Stone

Very powerful and convenient to use!

10 Nov 2013 Slavi

Now thank you very much, your second option works perfect. This is what I need. I wish I can give you 5 stars now :).
You should include this in the description. It's really helpful :).
Thank you one more time

10 Nov 2013 Rob Campbell

Slavi, the behavior you see is is typical of functions such as this because each line is made up of multiple plot elements and the legend command doesn't know anything about that. There are two ways for you to proceed. Firstly, use the "axes" command in order to make a set of legend axes, then build your own legend within these axes. It'll be a bit of pain, but you get the most flexibility this way. Secondly, you can tell the legend command which plot elements to use:

hold on
>> A %This is what's in the handles

A =

mainLine: 174.0065
patch: 175.0050
edge: [176.0026 177.0026]


10 Nov 2013 Slavi

Very helpful :). But unfortunately I can't display only the Y-values in the legend. There are more additional data sets in the legend I can't delete.
Do you have any idea? :)

20 Feb 2013 cpas1940  
08 Feb 2013 uncung fgv

what is shade error bar? i try to execute it but cannot.

27 Jan 2013 Wantong  
07 Aug 2012 Diego  
10 Jul 2012 Franklin  
29 May 2012 German Gomez-Herrero  
02 Mar 2012 Agatha lenartowicz

Very helpful function. Thanks!

12 Apr 2011 Estella Liu  
07 Mar 2010 TIAN ZHOU  
12 Jan 2010

Correct bug that caused patch area not to be plotted when y contained NaNs and error bars were generated using the function handles approach.

18 Jan 2010

Improved error-checking of input args and revised some of the comments.

06 Feb 2014

Handles now only returned if the user requests them. Plot elements are re-ordered in a more elegant manner.

Contact us