Code covered by the BSD License  

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

» Watch video

Highlights from

5.0 | 34 ratings Rate this file 417 Downloads (last 30 days) File Size: 2.75 KB File ID: #26311 Version: 1.6
image thumbnail



Rob Campbell (view profile)


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, Boundedline.M, Fill Plot(Mat,Varargin), Superbar, and Errorshade.

MATLAB release MATLAB 8.5 (R2015a)
MATLAB Search Path
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (54)
10 Jun 2016 Rob Campbell

Rob Campbell (view profile)

People have reported a few issues recently that I haven't seen until now. I'll get around to updating stuff soon.

Comment only
03 Jun 2016 Mark Benoit

the left and bottom axes are a problem for me too. I just plotted a black line for the axes there. Not a great fix, but it works

Comment only
24 May 2016 Juan Carlos Moreno

Hey Philip thanks for your reply.

Yes I try with set(gca,'YScale','log') and I can see the lien but the shading is gone.

anyway to fix this?

Comment only
19 May 2016 Philip Cheney

The simplest way to change the plot to semilogy is to add
to the function. Unfortunately, it looks like the shading is lost in semilogy (though retained in semilogx), even though the error bounds are plotted. Is this an issue with patch?

Comment only
18 May 2016 Juan Carlos Moreno


I am trying to use this function but need to do it in a semilog plot (semilogy). Can you tell me if there is anyway to do so?


Comment only
02 May 2016 E. Ch3yn3t

Hi Rob,
I was wondering if it is possible to add an option in your function allowing to flip the x and y axis ? Otherwise, that's a great function !

Comment only
21 Apr 2016 giacomo valle  
21 Apr 2016 giacomo valle


Out of memory. The likely cause is an
infinite recursion within the program.

07 Mar 2016 Gokhan Kirlik  
04 Mar 2016 Marcin Konowalczyk

Some suggested updates:
- use 'narginchk' instead of 'nargchk'
- use uistack(H.main_line,'top'); instead of deleting and re-plotting the line

My short version (with some removed functionality):

function H = shplot(x,y,e,varargin)
%% shplot(x,y,e,opt,varargin)
% Striped down version of shplot
% Based on a code by Rob Campbell
% Written by Marcin Konowalczyk
% Timmel Group @ Oxford University

x = x(:); y = y(:);
isheld = ishold; if ~isheld; cla; hold on; end;
H.main_line = plot(x,y,varargin{:});

eu = y + e; el = y - e;
col = 0.15*get(H.main_line,'color') + 0.85; ecol = 3*col-2;
H.patch = patch([x ;flipud(x)],[el ;flipud(eu)],1,'facecolor',col,'edgecolor','none','facealpha',1);
H.edge_u = plot(x,eu,'-','color',ecol);
H.edge_l = plot(x,el,'-','color',ecol);

if ~isheld; hold off; end;

22 Feb 2016 Adam

Adam (view profile)

27 Jan 2016 Luis Felipe


19 Nov 2015 Douglas

The lines and the shaded region are not the same color for me, as in the example.

Comment only
10 Sep 2015 Yasamin Khorramzadeh

Thanks for the helpful function. However, it seems like the shaded colors disappear when I go to logscale? Is there a way to fix this?

28 Aug 2015 Ciara Devine

Sorry I meant to say left and bottom axes lines rather than x axes.

28 Aug 2015 Ciara Devine

Thank you for this function! However when I set my shaded errors bars to be transparent the left and bottom x axes disappear. They reappear if I go set(gca, 'linewidth', 2) thereby making the axes thicker, but the right and top axes then are disproportionately think.

Has anyone figured out how to avoid the axes disappearing or bring them back once they've disappeared?

Comment only
19 Aug 2015 Matt

Matt (view profile)

Great, thanks!

06 Aug 2015 Hugo Gonzalez Villasanti  
06 Jul 2015 Alberto Jr

Very new to Matlab and do not understand how to use this in my code? Can anyone explain step by step how to use it? I spent hours and I haven't succeeded in plotting one graph yet. Thank you

Comment only
05 Jun 2015 Diana

Diana (view profile)

YMusic: This is what I do to save the figure.
h=figure, shadedErrorBar(Ctrl(:,1), Ctrl(:,2),Std_Ctrl(:,2),'-k', 1)
hold on,
shadedErrorBar(T5(:,1), T5(:,2), Std_T5(:,2),'-y',1)


I hope it is useful for you.

Comment only
02 Jun 2015 YMusic

YMusic (view profile)

Fantastic plots, BUT.... I can't get savefig or print to work... in your description you say to "plot2svg (FEX submission 7401) if you wish to get high quality printed files", but do you know somewhere that I could find an example of you doing this? I can't get it to work and it says that it can't save a struct. I tried changing it using num2cell and struct2table. I have ~200 shadederrorbars on one figure. Any guidance is appreciated. Thank you in advance!

23 Apr 2015 korkam

korkam (view profile)

13 Apr 2015 Fernando Andreotti  
09 Apr 2015 Rob Campbell

Rob Campbell (view profile)

Mich: the command returns the handles to the plot objects so to increase line thickness you can modify the plot object properties:

Comment only
04 Apr 2015 Sergei Paleichuk  
04 Apr 2015 Mich

Mich (view profile)

I managed to add legend for my graph. I missed out one of Campbell's previous comment.

Here is it for those who might face the same pbl! :
hold on
>> A %This is what's in the handles

A =

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


Comment only
04 Apr 2015 Mich

Mich (view profile)

Thank you so much for this code!! Helped me alot! :D
Can i check how should i add legend and increase the thickness of the main line? I tried reading the comments to get some help but i dont quite uds.

Comment only
02 Apr 2015 Stefan Grandl  
05 Mar 2015 Rob Campbell

Rob Campbell (view profile)

Sarah -- try the boundedline submission listed on the "This file inspired" bit in the File Information, above.

Comment only
05 Mar 2015 Sarah S.

Thank you for this function. How can I do the same thing if the shaded variable is the x and not y? (plot of altitude vs concentration for example, where the concentration has a range of values to be calculated with the shadederrorbar function)

16 Jan 2015 Kay

Kay (view profile)

Very nice, very helpful.
I have added ",'HandleVisibility','off'" to the patch and the edge lines (code lines 146,150 and 151). In that way, one can use "legend" in a created plot, and only the main lines are listed.

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

Tihana (view profile)

very helpful! Thank you.

25 Apr 2014 Irene Seco  
05 Mar 2014 Alex

Alex (view profile)

20 Feb 2014 Rob Campbell

Rob Campbell (view profile)


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

Comment only
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

Comment only
13 Jan 2014 Rob Campbell

Rob Campbell (view profile)

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

Comment only
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?

Comment only
12 Dec 2013 Stone

Stone (view profile)

Very powerful and convenient to use!

10 Nov 2013 Slavi

Slavi (view profile)

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

Rob Campbell (view profile)

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]


Comment only
10 Nov 2013 Slavi

Slavi (view profile)

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.

Comment only
27 Jan 2013 Wantong  
07 Aug 2012 Diego

Diego (view profile)

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

TIAN ZHOU (view profile)

12 Jan 2010 1.3

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 1.4

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

06 Feb 2014 1.5

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

23 Apr 2015 1.6

Verified to work with R2015a

Contact us