How to plot a grouped bar chart with errors bar as shown in the figure?
Asked Answered
F

2

6

I am trying to plot a grouped bar chart like the one in the figure below. I found the errorbar() function, but so far I cannot figure it out how to make it.

Here is my code.

Y = [0.9322225 0.86225 0.8973;
     0.8953635 0.862868 0.8099605;
     0.7473585 0.675698 0.80484];

bar(Y, 'grouped')
bar(Y, 'BarWidth', 0.5);
set(gca,'XTickLabel',{'', 'ML', '',  'HSV', '', 'NCC'});

Credit: This figure is from the work of Sanin et al. "Shadow Detection: A Survey and Comparative Evaluation of Recent Methods."

Bar chart

Ferromagnesian answered 10/1, 2013 at 11:1 Comment(2)
Please post some code to show what you have tried so far. I'm sure you can at least a bar chart to plot, probably a grouped one.Mezereum
Hi Dan, thank you for mentioning. I've edited my post.Ferromagnesian
E
7

enter image description hereenter image description here ![

clc;
close all;

%Suppose you have the following data for five different strains across 4
%different experimental conditions (Conditions A,B,C,D, from left to right)


Strain1_Mean=[1.0 1.5 2.0 1.5]; % data
Strain2_Mean=[2.0 2.5 2.0 2.5]; % data
Strain3_Mean=[1.0 0.5 1.0 3.5]; % data
Strain4_Mean=[1.2 2.5 1.0 1.5]; % data
Strain5_Mean=[2.1 1.3 2.0 2.5]; % data

Strain1_std=[0.15 1.3 0.5 0.4];    %errors in data
Strain2_std=[1.0 1.5 0.2 0.5];     %errors in data
Strain3_std=[1.3 0.4 0.3 0.5];     %errors in data
Strain4_std=[0.13 1.36 0.17 0.45]; %errors in data
Strain5_std=[1.8 1.1 0.25 0.38];   %errors in data

% for asymmetric errors:

barwitherr(cat(3,zeros(4,5),[Strain1_std' Strain2_std' Strain3_std'...
    Strain4_std' Strain5_std']),[1 2 3 4],[Strain1_Mean' Strain2_Mean'...
    Strain3_Mean' Strain4_Mean' Strain5_Mean'],'LineWidth',2,...
    'BarWidth',0.8)


%for symmetric errors:

% barwitherr([Strain1_std' Strain2_std' Strain3_std' Strain4_std'...
%    Strain5_std'], [1 2 3 4],[Strain1_Mean' Strain2_Mean'...
%    Strain3_Mean' Strain4_Mean' Strain5_Mean']) 



set(gca,'XTickLabel',{'Group A','Group B','Group C','Group D'})
   legend('Parameter 1','Parameter 2','Parameter 3','Parameter 4',...
       'Parameter 5')
    ylabel('Value')
    grid on
   colormap summer

if you want to add more data sets, change zeros(4,5) to zeros(4,...) 6,7,8... 

I don't know how implement circles and squares like in example, if somebody can do it, you are welcome to improve :)][3]
Envisage answered 19/12, 2013 at 10:28 Comment(0)
M
6

I'm not 100% how to replicate that graph but maybe this will help you get started.

I found this on the File Exchange: http://www.mathworks.com/matlabcentral/fileexchange/30639-bar-chart-with-error-bars/content/barwitherr.m, give that a try in for plotting a bar chart with error bars. It allows for asymmetrical error bars and I reckon that if you want to make the markers different (like the squares and circles in your example) then you need to alter this line in the code in the link: errorbar(mean(x,1),values(xOrder,col),lowerErrors(xOrder,col), upperErrors(xOrder, col), '.k')

to something like:

errorbar(mean(x,1),values(xOrder,col),lowerErrors(xOrder,col), 'ok');
errorbar(mean(x,1),values(xOrder,col), upperErrors(xOrder, col), '*k')

Also if you want all three groups to have labels try set(gca,'XTickLabel',{'ML', 'HSV', 'NCC'});

Also check out colormap(summer) and help legend

Mezereum answered 10/1, 2013 at 13:29 Comment(1)
Thank you very much. I'm looking into it now.Ferromagnesian

© 2022 - 2024 — McMap. All rights reserved.