File Exchange

image thumbnail


version (1.53 KB) by Simon Musall
Short code to create shading around a mean. Input is a matrix with rows of observations.


Updated 01 Dec 2010

View License

usage: stdshade(amatrix,alpha,acolor,F,smth)
plot mean and standard deviations(std) coming from a matrix of data, at which each row is an observation. std can be changed to standard error of the mean(sem) in the code. sem/std is shown as shading.

- acolor defines the used color (default is red)
- F assignes the used x axis (default is steps of 1).
- alpha defines transparency of the shading (default is no shading and black mean line)
- smth defines the smoothing factor (default is no smooth)

Comments and Ratings (17)

Anoop B J


I am having a problem with setting the x axis right. Other things seem to work fine. But the x axis is only drawn between 1 and 2. Could you help out with that please?

Below is the data I am working with. I want to plot a scatterplot with shaded confidence intervals.

a = [146.146 154.006
144.498 148.602
117.563 145.987
108.53 113.418
109.366 119.78
128.15 132
115.02 120.584
122.88 125.69
140.109 150.013
99.347 100.333
112.768 113.58
104.246 114.114

When I try

the x axis is between 1 and 2. I am not able to understand how to make the plot to be the same length as my data. I think I need to give the value of F to be appropriate, but I am not clear with that. Could you help me out with this please?

thanx Simon, it is working very good for me. there is one more question, can i plot time series of different length e.g x = 10x95 y = 10:95 and z = 10x145
second i still want to know abot...'' F,smth'' in this code

Simon Musall

It seems like the easiest would be to call the function repeatedly and use the 'hold' command to plot multiple series with different colors.
Try this code for that:

h = figure;
x = cumsum(randn(10,95),2);
y = cumsum(randn(10,95),2)*2;
z = cumsum(rand(10,95),2)/2;
axis square; xlim([1 95]);

Hope that helps!

hi Simon Musall, thanx for your reply, i want to plot multiple series with different colors of stdshade, but it not working for me
stdshade(amatrix,alpha,acolor,F,smth), i don't know but i'm understanding how to change alpha, acolor,F,smth values
because within function it's not working
I have x = 10x95, y = 10x95 and z = 10x95 data and i want to plot these 3 variable with different shading color,

Simon Musall

Hi Asher,
happy to help. If you use a 10x95 matrix the line shade is defined by the standard deviation across rows, so there can be only 1 for all 10.
Could you clarify what shading you would like when plotting 10 lines instead?

hi, can someone give example to use this code; because it's not plotting multiple line.
i plot 10x95 data... and i give me single line shade
i want to plot 10 lines shaded

It is of great help! Thank you!

Simon Musall

Hi Robert, glad you like it! It's been a long time since I generated that figure and I don't have that code anymore. It's only using the 'hline/vline' command to draw the lines ( and a legend with the northwest location.


Nice function, Simon. Wondering if you could make available the scripts that you used for your examples?

Feihong Liu



Simon Musall

Hi Thorsten,

thanks for your suggestion. Apparently, the axis can also vanish under Win7. I think, the reason for this is the stretch-to-fill behavior (which is also disabled when changing the view-angle). I thought about adding this fix into the code but unfortunately it creates new problems e.g. when doing subplots. If someone has the axis problem, it therefore seems to make most sense to find a individual solution which produces a figure that fits the needs best. I found it useful to change the axis aspect ratio to disable stretch to fill:


Hi...thanks, works great, looks great!
except: On Mac (and Linux) the axes vanish once the shaded areas are introduced. (This seems to be connected with the OpenGL renderer that is used for alpha values below 1). A work around is to change the view-angle just a little by adding this line somewhere after stdshade is called:

Simon Musall

Thanks for your suggestions.

I'm not quit sure if I understand your changes though. When you change the size to the fir dimension, it gives you the amount of observations instead of their actual length. When I try to change it that way, the code breaks as F and the mean of observations has not the same length anymore.

Have you tried using amatrix' as an input instead? Maybe you are using a different matrix structure as I do.

Jeff Tuhtan

Useful file, but a few changes can be made, as it did not work at the onset:

if exist('F','var')==0 || isempty(F);

The above code had returned the size of the columns.



The above code had used nanmean, which calculated the mean of the whole dataset, resulting in only a point being plotted.


fixed known spelling errors

Minor changes:
shorter name
mean line uses selected color
transparency can be excluded

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

Inspired: errorshade