Get from Ico-github-logo

Highlights from

  • boundedline(varargin)
    BOUNDEDLINE Plot a line with shaded error/confidence bounds
  • inpaint_nans(A,method)
  • inpaint_nans_bc(A,method,...
  • outlinebounds(hl, hp)
    OUTLINEBOUNDS Outline the patch of a boundedline
  • inpaint_nans_demo_old.m
    Surface fit artifact removal
  • methods_of_inpaint_nans.m
  • inpaint_nans_demo.m
    Surface Fit Artifact Removal
  • test_main.m
    Repair to an image with 50% random artifacts
  • View all files
4.8 | 23 ratings Rate this file 247 Downloads (last 30 days) File Size: 399 KB File ID: #27485
image thumbnail



Kelly Kearney (view profile)


05 May 2010 (Updated )

Plot one or more lines with a shaded boundary (can represent error, confidence intervals, etc).

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information

Plot line(s) with error bounds/confidence intervals/etc. in Matlab


Confplot, Errorfill, and Shaded Error Bar inspired this file.

MATLAB release MATLAB 7.5 (R2007b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (36)
13 Jan 2015 Stephen Politzer-Ahles

That fixed it, thanks!

12 Jan 2015 Kelly Kearney

Kelly Kearney (view profile)


My guess is that OpenGL is the culprit. For pre-2014b, see workarounds in the documentation under Figure Properties > Renderer. This is a Matlab bug, not unique to this function. The problem should be resolved with the new handle graphics (R2014b+).

Comment only
12 Jan 2015 Stephen Politzer-Ahles

Thanks for this great function! I am having some problems, though, with combining this function and a colorbar on the same figure when I use the 'alpha' option. For example, the following code for me produces a lot of gibberish numbers next to the colorbar:

x = linspace(0, 2*pi, 50);
y1 = sin(x);
y2 = cos(x);
boundedline(x, [y1;y2], rand(length(y1),2,2)*.5+.5, 'alpha');

Whereas there is no problem if I don't use 'alpha':

x = linspace(0, 2*pi, 50);
y1 = sin(x);
y2 = cos(x);
boundedline(x, [y1;y2], rand(length(y1),2,2)*.5+.5);

(Note: I know in this example there's no real need to have a colorbar anyway. For real purposes, though, I often make plots which have something like this [] on a subplot and a boundedline on another subplot; this becomes an issue then)

(Also note: I get the same problem when using shadedErrorBar.m)

03 Oct 2014 Stephen

Thanks for the submission! Made my day SO much easier!

15 Aug 2014 Benjamin Pittman-Polletta

To get boundedline to treat nans the same way 'plot' does - i.e., leaving a blank space above where the nans appear - I inserted the following code at line 268:

% Splitting lines w/ nans in them.

plotdata_supp = cell(0, 7);
is_nan = zeros(size(plotdata,1),1);

for k = 1:size(plotdata,1)

naninds = isnan(plotdata{k,1}) | isnan(plotdata{k,2});

if any(naninds)

is_nan(k) = 1;

notnaninds = [0 ~naninds 0];
notnanstarts = find(diff(notnaninds) == 1);
notnanends = find(diff(notnaninds) == -1) - 1;

nopolys = length(notnanstarts);

for l = 1:nopolys

poly_indices = notnanstarts(l):notnanends(l);

xdata = plotdata{k,1}(poly_indices);
ydata = plotdata{k,2}(poly_indices);
lodata = plotdata{k,6}(poly_indices);
hidata = plotdata{k,7}(poly_indices);

plotdata_supp = [plotdata_supp; {xdata ydata ... plotdata{k,3:5} lodata hidata}];




plotdata = [plotdata(~is_nan, :); plotdata_supp];

05 Aug 2014 Pierre Mégevand  
29 Apr 2014 Irene Seco  
25 Apr 2014 Katherine  
02 Feb 2014 Kelly Kearney

Kelly Kearney (view profile)


The cmap input option should allow you to specify the colors you want to use for the lines, so that you can maintain the correlation between line and patch. For example:

[hl,hp] = boundedline(1:10, rand(10,1), 0.1, 'cmap', [0.3 0.2 0.5]);
set(hl, 'linewidth', 3);

Line style, etc. still need to be modified after the fact; color is treated specially due to the link between line and patch that you point out.

Comment only
01 Feb 2014 Omar Choudary

Very nice script.

However, for my particular application (and perhaps for others), it would be very useful to allow the possibility of giving the line properties individually, as for plot. That is, I want to give something like:

boundedline(..., 'Color', [0.3 0.2 0.5]);
or boundedline(..., 'LineWidth', 3)

instead of the string format option (e.g. 'r-').

The advantage of that would be that I can have my own table of colors, line specs, etc. specific to each line I want to plot and I want to use that with your function to specify the properties in advance. Otherwise it is very difficult to change the color afterwards without making it totally uncorrelated (and useless) with the color of the patches you create.

13 Jan 2014 Bass

Bass (view profile)

Lovely function! makes pretty plots. Sadly I haven't yet found an easy way to save plots with these patches in a vectorized form. My two cents regarding NaNs. While I agree there is clearly some ambiguity as to what to do with NaNs. The complete lack of support is not consistent with the basic behavior of existing plotting functions like 'line.' Which one would like to seamlessly substitute for a function like this.
'Line' introduces a discontinuity at NaNs This implementation may be slightly more difficult with patches. But it has the benefit of both being consistent with the meaning of a NaN and very convenient for formatting plots (e.g. when the user desires to easily separate an otherwise contiguous line.)

17 Dec 2013 Kelly Kearney

Kelly Kearney (view profile)


I intentionally did not include a workaround for NaN/Inf values in the bounds because it seems that everyone I've asked prefers a different method of dealing with these (drop the points, interpolate at those locations, substitute 0 for NaNs or a suitably large value for Infs, etc.). So instead, I leave it for the user to preprocess their data appropriately.

Comment only
28 Nov 2013 Eelke Spaak

Thanks for this great and simple submission. I have noticed one problem though, it cannot handle NaNs in the data. Matlab's plot() and line() simply ignore NaN values, but patch() does not draw anything when there are NaNs in the data.

To fix this, I added the following around line 313, right before the actual calls to line() and patch():

% remove nans
for k = 1:numel(xp)
naninds = isnan(xp{k}) | isnan(yp{k});
xp{k} = xp{k}(~naninds);
yp{k} = yp{k}(~naninds);

Comment only
21 Nov 2013 David McElroy

Great little submission.

I am having issues with the 'alpha' option. This file works fine on my work computer, but crashes matlab on my laptop and I am stuck overseas and desperate! Looking at the problems other users had with 'alpha' maybe it's something to do with opengl?


Comment only
01 Nov 2013 Ben Curtin

@Kelly - Thanks! I had no idea and will look into it some more.

Comment only
01 Nov 2013 Kelly Kearney

Kelly Kearney (view profile)


Unfortunately, the OpenGL renderer is the only one that supports transparency, but it does not support logarithmic axes (see, under "OpenGL vs. Other MATLAB Renderers"). If you need transparency, your best bet is probably to log-transform your data prior to plotting.

Comment only
31 Oct 2013 Ben Curtin

Thanks for writing this! I'm having an issue with the bounded line not being transparent when setting the x-axis to a log scale with:


I also have tried different opengl settings with no luck.

20 Oct 2013 Chris Jones

Thank you, this is quite useful! I appreciate the transparency feature.

23 Sep 2013 Ali Hummos

@ Kelly,
Thanks a million. I only had to switch off hardware accelerated opengl and the problem disappeared.
Basically, I only had to issue the command:
>>> opengl software
and it was fixed!

Comment only
17 Sep 2013 Kelly Kearney

Kelly Kearney (view profile)


My guess is that your OpenGL renderer is the culprit. I've gone through several computers in my Matlab-using time, including PCs and Macs, and always have issues of some sort with this (note that in my example screenshot, my axes box also goes missing... that's computer-specific). You can try some of the workarounds here:

Comment only
12 Sep 2013 Ali Hummos

Great work! It looks beautiful too.
Something happened however, I think when I updated to Matlab 2013, and now when I use the 'alpha' arugment the y axis line disappears!
Anyone had this issue??
Not sure how to resolve.

02 Apr 2013 Warwick  
28 Feb 2013 Daniel Lopes

Daniel Lopes (view profile)

Works like a charm! Great code.

17 Feb 2013 Kelly Kearney

Kelly Kearney (view profile)

@Gideon and Martin,

There's nothing in this function preventing you from setting the axis to a log scale via 'xscale' and 'yscale' properties, assuming all your inputs lead to positive values for the boundaries (like NaNs and Infs, negative values displayed on a log scale will cause the bounds patch to disappear). If you are looking for something more complex than that, please send me an example and I'll look into it.

Comment only
15 Feb 2013 Martin Nors Pedersen

Great function - very useful

like Gideon Simpson i would to plot some of my data on log scale - is this possible using this function?

when i try the bounderies are same color as the line so it is difficult to distinquish what is the mean and what is the stdev :)

20 May 2012 Gideon simpson

Works well, but is it possible to put one of the axes on a log scale?

30 Jan 2012 Mohammed Alwesabi  
07 Oct 2011 dbgtjp

dbgtjp (view profile)

excellent work.
if anybody else wants to avoid the patches appear in the legend simply add the following code after line 314.


this does the trick.

07 Oct 2011 dbgtjp

dbgtjp (view profile)

26 Aug 2011 Vassilis Kehayas  
26 Mar 2011 Felipe G. Nievinski

good job in pointing out advantages over several other similar entries on the FEX.

Comment only
06 Jan 2011 Il

Il (view profile)

Awesome. It works perfectly on the screen.
The only problem for me is that the transparent color cannot be exported with vector graphics pdf. If opengl renderer is used, it will convert it to raster, which is not so desirable.

11 Aug 2010 Alexander  
10 Aug 2010 Alexander

I experienced the following problem on my mashine with nVidia graphics:
When using the 'alpha' option the axes are overlayed. This a problem caused by the OpenGL rendering.
Workaround: activate software rendering by
opengl software;

Comment only
09 Jun 2010 Dylan

Dylan (view profile)

This is a very useful function that does almost exactly what I was trying to do, and it should save me a lot of time. Thanks for making it!

I think, though, that it would be even more useful if you made two modifications:

1) The reference color of the bounded area should be set independently (as an RGB array), rather than being constrained to be a lighter shade of the corresponding line color. This would be particularly relevant for users preparing b&w figures for publication who, for example, might want to use two black lines and two different shades of grey for the bounded areas. The specific example I provided could of course be facilitated simply by allowing separate transparency parameters for each bounded area, but that still would not provide as much flexibility as some users might want.

2) The default behavior of 'legend' is to use shaded boxes that are the same colors as the bounded areas. Some users might prefer that the legend use lines of the same color and style as the corresponding data. It would be nice to have the option of using either. I believe this could be controlled from within boundedline.m by setting the 'IconDisplayStyle ' property of the 'LegendEntry' array of each patch to 'on' or 'off'.

Example code:

Also, there appears to be a bug (using R2007b): adding a legend to a figure that was made using the 'alpha' option results in no legend and no bounded areas.

19 May 2010 Zachary Danziger

Nice file. I enjoy the flexibility.

Comment only
30 Jan 2013

Added outlinebounds.m function to allow outlining of patch objects (can be useful to see overlap when using opaque bounds).

09 Jun 2014

Per user request, function now interpolates across NaNs in the boundary patches.

10 Jul 2014

Updated for R2014b graphics

31 Jan 2015

Linked entry to GitHub

Contact us