Back to Code

cLine

cLine plots lines of data with error bars. A data cell-array is expected, with a matrix per cell containing the observation per row. The mean over rows is plotted as line segments. Example for inputs:

X = [1 2 4];
data{1} = [30 35 50; 29 27 45; 29 20 49];
data{2} = [10 60 11; 11 89 40; 9 50 20];
figtitle = 'cLine Plot';
xtitle = '';
ytitle = 'Y-title';
xticklabels = {'Session 1', 'Session 2', 'Session 3'};
legend_strings = {'Group 1', 'Group 2'};
colormatrix = [0 0 0; 1 0 0.75];
cLine(X, data, figtitle, xtitle, ytitle, xticklabels, legend_strings, colormatrix);
Here's the code to copy-paste:
function cLine(X, data, figtitle, xtitle, ytitle, xticklabels, legend_strings, colormatrix)

% function cLine(X, data, figtitle, xtitle, ytitle, xticklabels, legend_strings, colormatrix)
%
% X = [x1 x2 ... xn];
% data{lines} = [subjects x points]
% figtitle, xtitle, ytitle = '...'
% xticklabels{x-points} = '...'
% legend_string = '...'
% colormatrix: [lines x RGB];
%
% Thomas E. Gladwin & Matthijs Vink, 2008.

fid = figure;
p = get(fid, 'Position');
p(4) = p(3);
set(fid, 'Position', p);
lines_for_legend = [];
for iLine = 1:length(data),
    m = mean(data{iLine}, 1);
    se = (var(data{iLine}, 1) .^ (1/2)) / sqrt(size(data{iLine}, 1));
    % Mean line
    for seg = 1:(length(m) - 1),
        l = line([X(seg) X(seg + 1)], [m(seg) m(seg + 1)]);
        set(l, 'Color', colormatrix(iLine, :));
        set(l, 'LineWidth', 2);
    end;
    lines_for_legend = [lines_for_legend l];
    % Error bars
    for point = 1:length(m),
        x = X(point);
        w = mean(diff(X)) / 20;
        yc = m(point);
        yu = yc + se(point);
        yd = yc - se(point);
        l = line([x x], [yd yu]);
        set(l, 'Color', colormatrix(iLine, :));
        l = line([x - w x + w], [yd yd]);
        set(l, 'Color', colormatrix(iLine, :));
        l = line([x - w x + w], [yu yu]);
        set(l, 'Color', colormatrix(iLine, :));
    end;
end;
xl = xlim; if diff(xl) == 0, xl(2) = xl(1) + 1; end;
yl = ylim;
for iLine = 1:length(data),
    m = mean(data{iLine}, 1);
    % Square
    for point = 1:length(m),
        x = X(point);
        y = m(point);
        wh = mean(diff(X)) / 20;
        wv = wh * diff(yl) / diff(xl);
        patch([x - wh, x - wh, x + wh, x + wh], [y - wv, y + wv, y + wv, y - wv], colormatrix(iLine, :));
    end;
end;

legend(lines_for_legend, legend_strings);
set(gca, 'XTick', X);
set(gca, 'XTickLabel', xticklabels);
fs = get(gca, 'FontSize');
set(gca, 'FontSize', 1.5 * fs);
t = title(figtitle);
set(t, 'FontSize', 1.5 * fs);
t = xlabel(xtitle);
set(t, 'FontSize', 1.5 * fs);
t = ylabel(ytitle);
set(t, 'FontSize', 1.5 * fs);