Code covered by the BSD License  

Highlights from
boundedline: line plots with shaded error/confidence intervals

4.75

4.8 | 21 ratings Rate this file 278 Downloads (last 30 days) File Size: 7.51 KB File ID: #27485
image thumbnail

boundedline: line plots with shaded error/confidence intervals

by

 

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
Description

This function allows you to plot one or more lines with their error range/confidence intervals/whatever bounds you require. It provides an advantage over several other similar entries on the FEX in that it:
1) allows x-y input similar to plot, where one call can create multiple lines at once, either by listing consecutive x-y pairs or by using using matrices for x and/or y.
2) can add bounds in either the x- or y-direction, leading to support of plots where the x axis represents the dependent variable

3) can render the shaded bounds either with transparency or as a lighter opaque patch, allowing flexibility with different renderers (helpful when OpenGL acts up, as it often does on my own computer).

4) Can use linespec definitions, a colormap, or the default color order, as well as varying color intensity for the shaded bounds, for flexible color of lines and bounds

5) returns handles of lines and patches for future modification if necessary

Acknowledgements

Confplot, Errorfill, Shaded Error Bar, and Inpaint Nans 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 (33)
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}];

end

end

end

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

@Omar,

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.

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

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

@Eelke,

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.

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);
end

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?

Thanks
Dave

01 Nov 2013 Ben Curtin

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

01 Nov 2013 Kelly Kearney

@Ben,

Unfortunately, the OpenGL renderer is the only one that supports transparency, but it does not support logarithmic axes (see http://www.mathworks.com/help/matlab/ref/figure_props.html#Renderer, under "OpenGL vs. Other MATLAB Renderers"). If you need transparency, your best bet is probably to log-transform your data prior to plotting.

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:

set(gca,'XScale','log');

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

17 Sep 2013 Kelly Kearney

@Ali,

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

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

Works like a charm! Great code.

17 Feb 2013 Kelly Kearney

@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.

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

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

set(get(get(hp(iln),'Annotation'),'LegendInformation'),'IconDisplayStyle','off');

this does the trick.

07 Oct 2011 dbgtjp  
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.

06 Jan 2011 Il

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;

09 Jun 2010 Dylan

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:
set(get(get(hPatches(1),'Annotation'),'LegendInformation'),'IconDisplayStyle','off')

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.

Updates
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

Contact us