Code covered by the BSD License  

Highlights from
shadedErrorBar

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

shadedErrorBar

by

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
Description

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)
                    
  Purpose
  Makes a 2-d line plot with a pretty shaded error bar made
  using patch. Error bar color is chosen automatically.
                    
  Inputs
  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
           bar.
  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
                vectors.

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

Acknowledgements

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 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 (40)
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)

print(h,'-djpeg','-r600','FileName')

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: http://www.matlab-cookbook.com/recipes/0050_Plotting/0010_Plot_Manipulation/029_whatIsAHandle.html

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! :
A=shadedErrorBar(x,y,{@mean,@std},'-b',1);
hold on
B=shadedErrorBar(x,y+10,{@mean,@std},'-r',1);
>> A %This is what's in the handles

A =

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

legend([A.mainLine,B.mainLine],'A','B')

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)
Thanks!

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)

Constantinos,

Etsi?
y=randn(30,80)+10; x=1:size(y,2);
shadedErrorBar(log(x),y,{@median,@std},{'r-o','markerfacecolor','r'});

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')
http://www.mathworks.com/help/matlab/ref/figure_props.html

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:

A=shadedErrorBar(x,y,{@mean,@std},'-b',1);
hold on
B=shadedErrorBar(x,y+10,{@mean,@std},'-r',1);
>> A %This is what's in the handles

A =

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

legend([A.mainLine,B.mainLine],'A','B')

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)

 
Updates
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