Skip to content. | Skip to navigation

Personal tools
    di Bernardo Lab - Systems and Synthetic Biology Lab
    You are here: Home Internal Site DRY LAB MATLAB STUFF How to plot bars groups with error bars by Gianfranco

    How to plot bars groups with error bars by Gianfranco

    Hi! I've written a function to plot, using matlab, a graph with bars groups and the respective error bars You can find it in our group area at: ...\data\shared_documents\matlab\Gianfranco Matlab Code Repository\BarGroupErrors.m the code is also available below :-)


    function handles = BarGroupErrors(barvalues, errors, groupnames, g_title, g_xlabel, g_ylabel, g_legend)

    %this function plots a group of bars with the error bars using the functions

    %bar and errorbar.

    %barvalues is the m-by-n matrix with the bar values

    %errors is the m-by-n matrix with the errors values

    %m is the number of bars groups to plot

    %n is the number of bars for each group

    %groupnames is a cellstr vector of group names

    %g_title is the graph title

    %g_xlabel is the label of the x axis

    %g_ylabel is the label of the y axis

    %g_legend is a cellstr vector adopted to indicate the plot legend

    numgroups = size(barvalues, 1); % number of groups

    numbars = size(barvalues, 2);% number of bar for each group

    %plotting bar

    handles.bars = bar(barvalues, 1,'edgecolor','k', 'linewidth', 2);hold on;

    figure(gcf);hold on;


    %plotting errors

    for i = 1:numbars

    x =get(get(handles.bars(i),'children'), 'xdata');

    x = mean(x([1 3],:));

    handles.errors(i) = errorbar(x, barvalues(:,i), errors(:,i), 'k', 'linestyle', 'none', 'linewidth', 2);

    ymax = max([ymax; barvalues(:,i)+errors(:,i)]);


    %adding legend labels and title to the figure

    figure(gcf);hold on;

    handles.legend = legend(g_legend, 'location', 'best', 'fontsize',12);




    set(gca, 'xticklabel', groupnames, 'box', 'off', 'ticklength', [0 0], 'fontsize', 12, 'xtick',1:numgroups, 'linewidth', 2);

    grid on;

    hold off;