File Exchange

image thumbnail


version (8.45 KB) by Andreas Trier Poulsen
Creates a 2D plot containing multiple lines with pretty shaded error bars.


Updated 13 Feb 2015

View License

This is a tool for plotting multiple lines with pretty shaded error bars.
It is an extension of the popular shadedErrorBar, by Rob Campbell, enabling plotting of multiple data lines with overlapping errorbars.
The main improvements are:
* Optimised for the plot defult renderer instead of openGL, which is known to cause problems, e.g., with logarithmic axes and not being able to save figures as vector graphics in the eps-format.
* Turns off legends for all elements (e.g. edges and shaded area), except for the main lines.
* By default the edges of overshadowed patch areas are plotted in an non-obtrusive manner so all error bars can be seen, but avoids cluttering of the plot.
* The patches are plotted in reverse order to make sure the first entry is "on top".

Comments and Ratings (20)

I'm starting to use this tool and but I have two small problems:
1/ is about the legend: I used latex interpreter but it doesn't work,
2/ I wonder if we have the possibility to change the colors of the lines.
Thank you in advance

Mario Solis


mseb.m does not work when x and y are nx1 (column) vectors. The code converts the y data to a row vector, then complains that x and y do not have the same dimensions.


Hi, I would like to try the mseb, since I had problems to use the plots in adobe.

I work with 3 vectors 128x1 doubles.

- F 128x1 doubles, contains frequencies

- Im 128x1 doubles, contains coherency numbers

-Var 128x1 doubles, contains the variation of the coherencies.

For some reason, that I do not understand in line 94 the vector Im is changed => 94 y=y';

Thereafter it tells me that the vectors are not in the same size.

I order the inputs exactly as in the function shadederrorbars - which works well in matlab but has problems to exports, as you may know.

could someone clarify me this aspect?



Great work. Thanks!
When printing the output graphs as pdf and open it later with illustrator, I can't edit the file any more!
Any ideas...

Adam Ranson

Hi Andreas, great work. For Inkscape users out there a similar procedure can be used to that described below to get transparent filled error bars:

- Create the graphs with solid error bars
- Save in .eps
- Open with Inkscape
- Select one triangle of the error bar
- Use Edit->Select Same->Fill and Stroke
- Select Object->Fill and Stroke and set Opacity to for example 50%.

I have a 2D vector which forms a loop and crosses over itself. Essentially a hysteresis loop. When I plot the error along this loop, the error regions overlap and voids result. Can this be prevented? I would like to see the overlapping areas as either a different color or a darker color (EX: Like two translucent areas overlapping).


Hi Andreas, thank for the explanation! That's a good way to solve the issue


I found a very easy way to overcome the raster rendering of the .eps export with the shaded error transparency and keep the vector. May be that's a Columbus egg but it can be useful:

- Create the graphs with solid error bars
- Save in .eps
- Open with Illustrator (for example)
- Select one triangle of the error bar
- Use the function 'Select', 'Same', 'Fill Color'
- Add the transparency on the selection

With few extra steps you'll have the mseb graphs with transparency in vector graphics



Mario Solis

Mario Solis

Great, great work Andreas.
I would like to have a different lifestyle for each line (e.g. 'diamonds, squares, and a dotted lines'). Is it possible to do this without multiple calls for mseb?

Hi Alberto

Thank you for your feedback.

You can choose to plot using multiple calls to mseb (see example (2) below), but I created mseb to avoid this and be able to plot multiple lines in the same plot with one call.

Also, mseb plots the lines, shaded error bars and their borders in a specific order to ensure that all can be seen where they overlap. I you plot each line with a seperate call to mseb, it can't do this, and you risk lines being covered by the shaded bars from lines plotted afterwards.

This is especially important if you use the default renderer (important if you save as vector graphics, such as eps.).

Here are two examples plotting the same data with one or three calls to mseb:

% Matlab examples
% Generating three lines collected into one matrix:
x = 1:100; y = randn(1,100,30)*10;
t = (1:100) - 50; y(2,:,:) = repmat(t,[1,1,30]);
y(2,:,:) = y(2,:,:) + 0.06.*y(2,:,:).^2 + randn(1,100,30)*10;
y(3,:,:) = repmat(t,[1,1,30]);
y(3,:,:) = 60 - abs(y(3,:,:))+ randn(1,100,30)*10;

% (1) Plot all at once
lineProps.col{1} = 'b';
lineProps.col{2} = 'r';
lineProps.col{3} = [0 0.5 0];

figure; title('All at once'),
H = mseb(x,mean(y,3), std(y,[],3), lineProps);

% (2) Plot using three calls to mseb
lineProps1.col{1} = 'b';
lineProps2.col{1} = 'r';
lineProps3.col{1} = [0 0.5 0];

figure; title('One at a time using hold on')
hold on
H1 = mseb(x,mean(y(1,:,:),3), std(y(1,:,:),[],3), lineProps1);
H2 = mseb(x,mean(y(2,:,:),3), std(y(2,:,:),[],3), lineProps2);
H3 = mseb(x,mean(y(3,:,:),3), std(y(3,:,:),[],3), lineProps3);

Just a clarification regarding the lineProps.col: if I add the 3 cells as in you example

lineProps.col{1} = 'b';
lineProps.col{2} = 'r';
lineProps.col{3} = [0 0.5 0];

how can I recall specific color/cell in single function?

For ex.

H1 = mseb(x,y1,errBar1,lineProps.col{1,1},transparent)
H2 = mseb(x,y2,errBar2,lineProps.col{1,2},transparent)
H3 = mseb(x,y3,errBar3,lineProps.col{1,3},transparent)

so that
- function H1 plots x,y1 in blue
- function H2 plots x,y2 in red
- function H3 plots x,y1 in [0 0.5 0]



Perfect. Works simply and as advertised.

Hi Lyndon

Thank you for your feedback.

I have made some small change so you should be to define the colours using RGB values. You should even be able to mix the types as:
lineProps.col{1} = 'b';
lineProps.col{2} = 'r';
lineProps.col{3} = [0 0.5 0];

I hope this works.

Thanks this is perfect for what I need! Would you be able to add the ability to specify RGB color values? i.e. instead of lineprop.col={'g'} which is too bright of a green for my taste, i'd like to use lineprop.col={[0 0.5 0]}.

Hi Dominik

Thank you for providing feedback. I apologise for the late answer, but I seem to have forgot to put the file on my watch list.

The examples work when I run them on Matlab 2014a. Can you please tell me which version you are running?

I think I have had similar problems when calling the repmat function on older versions of matlab. I have therefore changed the way it is called, in hopes that this is the cause of your problems.

The new version have been updated. Can I get you to download it and see if it helped?



Following up on my first post: The repmat error comes from line 120 of your script.

I am simply trying to plot several time series + their std devs; they are stored in 2 matrices with the same dimensions (n x m). So I have a matrix m1 with mean values m for n time steps. I also have a matrix m2 with a std deviation per mean value along each time step.


yields said repmat error. Using the shadedErrorBar function on each time series alone does work properly. for example:


Any ideas what I am doing wrong here with your function? I'd love to be able to use it properly!

Thanks, this is exactly what I am looking for! Sadly I do have a little problem: everytime I try to use your function i get the following error:

Error using repmat
Too many input arguments.

I also get this error when I simply copy&paste your example! Can you maybe check your example?


Slight changes made to calls to "plot" so colours can be defined by using both strings and RGB vectors.
Also updated file description on this page.

Updated calls to the repmat function from using the repmat(A,P1,P2,...,Pn) format, to using repmat(A,[P1,P2,...,Pn]). This was done in hopes of supporting older versions of Matlab.

Changed screenshot.

MATLAB Release Compatibility
Created with R2014a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired by: raacampbell/shadedErrorBar

Inspired: errorshade, anomaly

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.

Learn About Live Editor