File Exchange

image thumbnail

Shaded area error bar plot

version 1.3.1 (2.05 KB) by Víctor Martínez-Cagigal
Error bar plot with filled area curves.


Updated 09 Oct 2018

View Version History

View License

This function plots the mean and standard deviation of a set of data filling the space between the positive and negative mean error using a semi-transparent background, completely customizable.

Input parameters:
- data: Data matrix, with rows corresponding to observations and columns to samples.
- options: (Optional) Struct that contains the customized params.
* options.handle: Figure handle to plot the result.
* options.color_area: RGB color of the filled area.
* options.color_line: RGB color of the mean line.
* options.alpha: Alpha value for transparency.
* options.line_width: Mean line width.
* options.x_axis: X time vector.
* options.error: Type of error to plot.
if 'std', one standard deviation;
if 'sem', standard error mean;
if 'var', one variance;
if 'c95', 95% confidence interval.
Example of use:
data = repmat(sin(1:0.01:2*pi),100,1);
data = data + randn(size(data));

Cite As

Víctor Martínez-Cagigal (2021). Shaded area error bar plot (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (13)

Mahmoud Abbaszadeh

it is a good function, but that would be great if you include an example that how can we use an x-axis different from the length of data.

Berta Martínez

Great function! I just have a suggestion: if you want to define the x data using options.x_data, you must also include all the other options fields (e.g. options.handle, options.color_area...), otherwise it gives an error. It happens the same with all the other options' fields If you add something similar to what you do for options.x_axis, you solve this problem. You can add for example:

if ~isfield(options,'handle'); options.handle=figure(1);end
if ~isfield(options,'color_area'); options.color_area = [128 193 219]./255; end
if ~isfield(options,'color_line'); options.color_line = [ 52 148 186]./255; end
if ~isfield(options,'alpha'); options.alpha = 0.5; end
if ~isfield(options,'line_width'); options.line_width = 2; end
if ~isfield(options,'error'); options.error = 'std'; end

Karl Neuhäuser

Nice idea!

Lawren Gamble


@Ahmad I just tested it and it works:

data = repmat(sin(1:0.01:2*pi),100,1);
data = data + randn(size(data));
data = repmat(sin(1.5:0.01:2*pi),100,1);
data = data + randn(size(data));
hold on

Ahmad Mahmood


This works well for one data set, but when i use hold on, only the second data set is plotted, not sure why?

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
plot_areaerrorbar(a), 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.


Grigory Tagiltsev

Rob Campbell

Might be neater with an input parser instead of the structure for the optional arguments:

Hyunryung Kim



Thank you. Excellent function.

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

Community Treasure Hunt

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

Start Hunting!