Adding variance as error bars on line plot

17 views (last 30 days)
I am plotting several datasets on a single figure where each point on the line is defined as the average of different runs of the same experiment.
It is simple enough to plot the lines of each datasets but is it also possible to include the variance either side of the average calculated at each data point on the lines?
Example: y1(1) = 1; y1(2) = 2; y1(3) = 3; %similar for y2 & y3 y = [mean(y1),mean(y2),mean(y3)]; x = [1,2,3]; plot(x,y); %How to add variance as shown as an error bar at y1, y2, y3....

Accepted Answer

Star Strider
Star Strider on 29 Aug 2018
Edited: Star Strider on 29 Aug 2018
There are probably several ways to do the plot, using the errorbar (link) function being the easiest.
You can calculate the variance using the var (link) function.
See the links in those documentation pages for related functions.
EDIT
A more useful metric than the variance is the standard error of the mean (SEM), calculated as the standard deviation divided by the square root of the number of observations. You can convert this to confidence intervals by multiplying it by the inverse value of the t-distribution with a given probability and degrees-of-freedon, given by the Statistics and Machine Learning Toolbox tinv (link) function.
Example
x = 1:10; % Create Data
y1 = rand(1,10); % Create Data
y2 = rand(1,10); % Create Data
y3 = rand(1,10); % Create Data
ym = [y1; y2; y3];
N = size(ym,1);
y = mean(ym);
SEM = std(ym) / sqrt(N); % Standard Error Of The Mean
CI95 = SEM * tinv(0.975, N-1); % 95% Confidence Intervals
figure
errorbar(x, y, CI95)
grid

More Answers (0)

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!