Show Sidebar Hide Sidebar

Error Bars in MATLAB

How to add error bars to a line, scatter, or bar chart. Seven examples of symmetric, asymmetric, horizontal, and colored error bars.

% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

x = 0:pi/10:pi;
y = sin(x);
e = std(y)*ones(size(x));

fig = figure
errorbar(x,y,e)

%--PLOTLY--%

% Strip MATLAB style by default!
response = fig2plotly(fig, 'filename', 'matlab-symmetric-error-bars');
plotly_url = response.url;
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

data = {...
  struct(...
    'x', [0, 1, 2], ...
    'y', [6, 10, 2], ...
    'error_y', struct(...
      'type', 'data', ...
      'array', [1, 2, 3], ...
      'visible', true), ...
    'type', 'scatter')...
};
response = plotly(data, struct('filename', 'basic-error-bar', 'fileopt', 'overwrite'));
plot_url = response.url
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

trace1 = struct(...
  'x', { {'Trial 1', 'Trial 2', 'Trial 3'} }, ...
  'y', [3, 6, 4], ...
  'name', 'Control', ...
  'error_y', struct(...
    'type', 'data', ...
    'array', [1, 0.5, 1.5], ...
    'visible', true), ...
  'type', 'bar');
trace2 = struct(...
  'x', { {'Trial 1', 'Trial 2', 'Trial 3'} }, ...
  'y', [4, 7, 3], ...
  'name', 'Experimental', ...
  'error_y', struct(...
    'type', 'data', ...
    'array', [0.5, 1, 2], ...
    'visible', true), ...
  'type', 'bar');
data = {trace1, trace2};
layout = struct('barmode', 'group');
response = plotly(data, struct('layout', layout, 'filename', 'error-bar-bar', 'fileopt', 'overwrite'));
plot_url = response.url
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

dfs = [0    4.5000    9.0000   13.5000   18.0000   22.5000   27.0000   31.5000];
Dt = [4.1681    4.1719    3.8631    4.1915    3.2843    3.2850    3.2308    3.2795];
R = [6.2272    7.4491    8.4765    7.7533    9.3598   12.9844   13.9740    9.2937] ;
SEM = [0.0142    0.0142    0.0127    0.0164    0.0183    0.0224    0.0193    0.023 ] * 100;

errorbar(dfs, Dt, SEM, 'ro');
hold on
plot(dfs,R,'bo');
title('Error Bar on Scatter Plot');
hold off;

response = fig2plotly;
plotly_url = response.url;
Inspired from Matlab Forum
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

data = {...
  struct(...
    'x', [1, 2, 3, 4], ...
    'y', [2, 1, 3, 4], ...
    'error_y', struct(...
      'type', 'data', ...
      'symmetric', false, ...
      'array', [0.1, 0.2, 0.1, 0.1], ...
      'arrayminus', [0.2, 0.4, 1, 0.2]), ...
    'type', 'scatter')...
};
response = plotly(data, struct('filename', 'error-bar-asymmetric-array', 'fileopt', 'overwrite'));
plot_url = response.url
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

x=[2,4,8, 16,32,64, 128,256, 300];
y=[8.39365 3.4323 1.6234 1.447 0.7632 0.0984 0.05435 0.03455 0.1024];
e=[0.3148 0.0450 0.0864 0.0511 0 0.0016 0.0026 0.0014 0.0060]
line(x,y);
title(' Error Bars on Log Log Plot');
errorbar(x,y,e);

set(gca,'xscale','log');
set(gca,'yscale','log');

response = fig2plotly;
plotly_url = response.url;
Inspired from Matlab Forum
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

x_theo = linspace(-4, 4, 100);
sincx = sinc(x_theo);
x = [-3.8, -3.03, -1.91, -1.46, -0.89, -0.24, -0.0, 0.41, 0.89, 1.01, 1.91, 2.28, 2.79, 3.56];
y = [-0.02, 0.04, -0.01, -0.27, 0.36, 0.75, 1.03, 0.65, 0.28, 0.02, -0.11, 0.16, 0.04, -0.15]

trace1 = struct(...
  'x', x_theo, ...
  'y', sincx, ...
  'name', 'sinc(x)', ...
  'type', 'scatter');
trace2 = struct(...
  'x', x, ...
  'y', y, ...
  'mode', 'markers', ...
  'name', 'measured', ...
  'error_y', struct(...
    'type', 'constant', ...
    'value', 0.1, ...
    'color', '#85144B', ...
    'thickness', 1.5, ...
    'width', 3, ...
    'opacity', 1), ...
  'error_x', struct(...
    'type', 'constant', ...
    'value', 0.2, ...
    'color', '#85144B', ...
    'thickness', 1.5, ...
    'width', 3, ...
    'opacity', 1), ...
  'marker', struct(...
    'color', '#85144B', ...
    'size', 8), ...
  'type', 'scatter');
data = {trace1, trace2};
response = plotly(data, struct('filename', 'error-bar-style', 'fileopt', 'overwrite'));
plot_url = response.url
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

data = {...
  struct(...
    'x', [0, 1, 2], ...
    'y', [6, 10, 2], ...
    'error_y', struct(...
      'type', 'percent', ...
      'value', 50, ...
      'visible', true), ...
    'type', 'scatter')...
};
response = plotly(data, struct('filename', 'percent-error-bar', 'fileopt', 'overwrite'));
plot_url = response.url
% Learn about API authentication here: https://plot.ly/matlab/getting-started
% Find your api_key here: https://plot.ly/settings/api

data = {...
  struct(...
    'x', [1, 2, 3, 4], ...
    'y', [2, 1, 3, 4], ...
    'error_y', struct(...
      'type', 'percent', ...
      'symmetric', false, ...
      'value', 15, ...
      'valueminus', 25), ...
    'type', 'scatter')...
};
response = plotly(data, struct('filename', 'error-bar-asymmetric-constant', 'fileopt', 'overwrite'));
plot_url = response.url
Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.