File Exchange

image thumbnail

Plot average line

version 1.0 (10.2 KB) by

plotAverage plots an average line into the data.

3 Ratings



View License

plotAverage adds an average line into plotted data.

By default, plotAverage
- calculates the average along y (for given or estimated points along x)
- plots errorbars with the standard error of the mean
- discards outliers to calculate the robust average
- plots the average line into the data's axes (as opposed to a new figure)

All these options can be changed.

plotAverage returns the handles to the average line and the error bars as well as the x and y data of the line.

Comments and Ratings (7)

xiaofeng liu

Hi, could you please give one example to use this code? Thank you so much!

Hi Jonas,
This looks very useful. I have ten blue lines and I want to create an average of the 10 over the top, but when I try and use plotAverage it produces another graph in a new window with a range of different coloured lines on. Is there a way to just get the average line over the top of the original?


Jonas (view profile)

Hi Han,

In this case, I suggest you pass the data used to create the line groups as input to plotAverage.

If you do not have access to the data anymore, but each subgroup has its own color (or e.g. its own tag), you can use "findobj" to gather the line handles and extract x and y data. For example, to plot an average for the blue group:
blueH = findobj(gcf,'color','b');
xData = get(blueH,'xData');
yData = get(blueH,'yData');
xyData = [xData,yData]';

Han Wang

Hi Jonas,
This is great and really helpful!
Quick question is that what would need to do if I have four subgroups of data in one plot, and each subgroup contains multiples lines that need to be averaged.


juan (view profile)


Jonas (view profile)

Hi Dafni,

Here's an example:

x = 1:0.1:10;
cmap = jet(10);
% plot some lines
figure,hold on,
for i=1:10,
% add average, set robust-option to 0


Dafni (view profile)

is there any example on how to use the file plotAverage?

MATLAB Release
MATLAB 7.10 (R2010a)

Inspired by: Average plot of data in a figure

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Win prizes and improve your MATLAB skills

Play today