File Exchange

image thumbnail

raacampbell/shadedE​rrorBar

version 1.62 (5.86 KB) by

Creates an attractive shaded error region rather than discrete bars.

343 Downloads

Updated

Editor's Note: This file was selected as MATLAB Central Pick of the Week

shadedErrorBar is a MATLAB function that creates a continuous shaded error region around a line rather than discrete bars. The 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. You will need OpenGL to get transparency to work.

*** NOTE: If you have a question, you will get a response much faster if you post an Issue on the project's GitHub page: https://github.com/raacampbell/shadedErrorBar ***

Comments and Ratings (82)

Thanks. Really appreciated!

Charles Lu

This function is very useful, thank you so much! My only issue is that I cannot plot multiple shadedErrorBars in app designer. It will pop up in another figure window and I can't figure out how to hold on using the function. Could someone help with this? Thanks!

Rob Campbell

Rob Campbell (view profile)

Aditya - the code doesn't do that right now. It's a good suggestion. I'll implement it when I have time.

Rob Campbell

Rob Campbell (view profile)

Istvan, did you try the last input argument that handles transparency?

Joe

Joe (view profile)

Istvan Kiss

Thanks for this but can you let me know how can I plot in such a way that two plots do not completely cover each other but rather come out as on your example?

Thanks a lot for this function!!

Thank you for the function! Is there any way to save the figure in such a way that it enables changing it in illustrator? saveas(figure,path,'epsc') doesn't work...

Vinil T C

Great function!

Guy Bouvier

Very useful function, thanks a lot!! It saved me much time and lines of code :)

Aditya

Aditya (view profile)

Can you please provide a working example where we have asymmetric error bars?

Antonin Blot

Thanks for this great code!
I got some troubles using datenum values on the x-axis. I have data points every 0.5 seconds over 30 minutes, but I want to plot the actual time on x instead just a duration. With the duration from 0 to 30 minutes I got no problems, but with the datenum-values the error shades form triangular areas all over the plot. Did anybody experience something similar?

Robert

Robert (view profile)

Terrific function. Very ease to use. Thanks a huge lot!

Helen Smith

Great function, however, is there a way to use if for displaying x error bars?

Jack Williams

Great function!

I found that adding

if isdatetime(xP)
xP = datenum(xP);
end

at Line 144 meant that I could use datetime values as the x input.

orlyka

orlyka (view profile)

Great function, thanks!
One question - the graphs I get are very heavy (6 MB and more when saved to PDF).
Any ideas on how to make them smaller?

My bad, I didn't clear my workspace and it influenced the x and y.

Rob Campbell

Rob Campbell (view profile)

Evan, I updated the submission to a GitHub page that I think already has the change you mention. I think it's narginchk, not nargoutchk, that you mean.

Rob Campbell

Rob Campbell (view profile)

I'll check out the 2016a issue.

Evan Smith

Fantastic! Thanks!

Only, note that with MATLAB2016a you need to change line 53 to error(nargoutchk(3,5,nargin))

Matlab User

Flexible suggestion for a legend:

for i = ...

h=shadedErrorBar(x_i, y_i, {@mean,@std});
h.mainLine.DisplayName = name_i;

end

legend(findobj(gca, '-regexp', 'DisplayName', '[^'']'));

Is there a way to specify line and shading color outside of the base? The 'color' line property when specified as the [R G B] style is not accepted (or I am doing it wrong)

Rob Campbell

Rob Campbell (view profile)

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

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

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?

Philip Cheney

Juan,
The simplest way to change the plot to semilogy is to add
set(gca,'YScale','log');
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?

hi,

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?

thanks

E. Cheynet

E. Cheynet (view profile)

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 !

giacomo valle

giacomo valle

THIS FUNCTION DOESNT WORK:

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

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;
set(gcf,'renderer','painters');
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);

uistack(H.main_line,'top');
if ~isheld; hold off; end;
end

Adam

Adam (view profile)

Luis Felipe

Nice!

Douglas

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

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?

CDev

CDev (view profile)

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

CDev

CDev (view profile)

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?

Matt

Matt (view profile)

Great, thanks!

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

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.

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!

korkam

korkam (view profile)

F A

F A (view profile)

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

Sergei P.

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')

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.

Rob Campbell

Rob Campbell (view profile)

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

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!

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.

Nicolas

Michael

Super awesome. Easy to use. Beautiful graphics.

Tihana

Tihana (view profile)

very helpful! Thank you.

Irene Seco

Alex

Alex (view profile)

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'});

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

Rob Campbell

Rob Campbell (view profile)

Bernadette, set(gcf,'Renderer','Painters')
http://www.mathworks.com/help/matlab/ref/figure_props.html

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?

Stone

Stone (view profile)

Very powerful and convenient to use!

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

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')

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? :)

cpas1940

uncung fgv

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

Wantong

Diego

Diego (view profile)

Franklin

Very helpful function. Thanks!

Estella Liu

TIAN ZHOU

Updates

1.62

stars

1.62

github for questions

1.62

update description

1.61

move to GitHub

1.6

Verified to work with R2015a

1.5

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

1.4

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

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.

MATLAB Release
MATLAB 8.5 (R2015a)

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

» Watch video