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));
plot_areaerrorbar(data);
Víctor Martínez-Cagigal (2021). Shaded area error bar plot (https://www.mathworks.com/matlabcentral/fileexchange/58262-shaded-area-error-bar-plot), MATLAB Central File Exchange. Retrieved .
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Create scripts with code, output, and formatted text in a single executable document.
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.
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
Nice idea!
Excellent
@Ahmad I just tested it and it works:
data = repmat(sin(1:0.01:2*pi),100,1);
data = data + randn(size(data));
plot_areaerrorbar(data);
data = repmat(sin(1.5:0.01:2*pi),100,1);
data = data + randn(size(data));
hold on
plot_areaerrorbar(data);
Hi!
This works well for one data set, but when i use hold on, only the second data set is plotted, not sure why?
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
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.
Might be neater with an input parser instead of the structure for the optional arguments: https://ch.mathworks.com/help/matlab/ref/inputparser.addparameter.html
Thank you. Excellent function.