File Exchange

image thumbnail

stdshade

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

100 Downloads

Updated 24 Aug 2019

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)

Cite As

Simon Musall (2019). stdshade (https://www.mathworks.com/matlabcentral/fileexchange/29534-stdshade), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (23)

Simon Musall

Glad to hear its working for you!
To adjust the x-axis, you can create a vector F (4th input to the function) that defines the x-position for each datapoint. For what you describe, you could do - sRate = 2400; F = 1/sRate : 1/sRate : 2799393 /sRate; -to get the correct vector.
Best,
Simon

Naina

Hi Simon,
Thanks. It works perfectly fine for me.
One another question , For now, on x-axis it plots upto the scale 2799393. I want to divide it with the sampling frequency, which is 2400. so that x-axis will be 0-1166. How can i do that please.?

Simon Musall

Hi Naina,
the function should do what you are describing in your point1. It will plot a line of 2799393 points that is the average over the 15 subjects. The shading will show the standard deviation across subjects. If you would like another metric like the standard error of the mean or variance, you can change line #26 correspondingly.
To plot two different data sets, you can open a figure and use 'hold('on')' between the first and second function call to plot them on the same axis. Just use different colors when plotting them. To get the difference, you can just compute the mean yourself using 'mean1 = nanmean(data1,2);' which gives you the mean across subjects in data set 'data1'. Then just do something 'dataDiff = mean1 - mean2;' to get the difference between them and use the plot command in the same figure to visualize it.
Hope this helps,
Simon

Naina

Hi Simon,
1: I have the data in the form of 15x2799393, where each row is the data from one particular subject. I want to plot the mean of all the subjects and the variability of each individual subject's data. I.e the shaded area is supposed to depict the variability or in other words the blurred plot of each subjects data. I wonder if this program is doing that already, if not then what changes do i need to make.?

2: I have two sets of data, each of the same size (15x2799393), and i need to plot the mean of both data sets separately, and plot the difference as well. How can i do that please.?

Simon Musall

Hi Eitan,
to avoid this issue, I've updated the function and added a boxcar filter that replaces the smooth function.
Hope this will fix it for you.

@Anoop: Sorry for the late reply. The function returns two data points with shading because your matrix has two columns. It seems like you want to average over the second dimension instead which you can achive by transposing the input matrix.
Try using stdshade(a',0.2,'k')

eitan asher

Didn't work for me with Matlab 2019a
My input matrix is of type double and I keep getting this error message -
Undefined function 'smooth' for input arguments of type 'double'.

tried to dig in a bit but didn't solve the problem yet
Am I missing something?
thanks

Anoop B J

Hi,

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
stdshade(a,0.2,'k'),

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;
stdshade(x,0.5,'b');
hold(h.Children,'on');
stdshade(y,0.5,'r');
stdshade(z,0.5,'g');
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 (https://www.mathworks.com/matlabcentral/fileexchange/1039-hline-and-vline) and a legend with the northwest location.

Robert

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

Feihong Liu

Tamas

Gunter

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:

http://www.mathworks.com/help/techdoc/visualize/f4-24991.html

ThorBarra

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:
camva(gca,camva+(camva/1000))

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);
F=1:size(amatrix,1);
end

The above code had returned the size of the columns.

and

amean=smooth(amatrix,smth)';

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

Updates

1.20.0.0

Replaced smooth function with simple boxcar filter to avoid problems with function call.

1.10.0.0

fixed known spelling errors

1.5.0.0

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
Acknowledgements

Inspired: errorshade