1
votes

I am trying to plot a simple bar graph with 8 bars, all the even bars in one color, all the odd bars in a different color. Below is what I've done:

info = [mean1 mean2 mean3 mean4 mean5 mean6 mean7 nean8];
sky_blue = [86, 180, 233] / 256;
orange = [230, 159, 0] / 256

for d=1:length(info)
plot_bar(d)=bar(info(1,d), 'BarWidth', 0.5); 
    if mod(d,2)==0
       set(plot_bar(d),'FaceColor', sky_blue);
    else 
       set(plot_bar(d),'FaceColor', orange);
    end
hold on;
end

For some reason, the bars are stacked vertically, one on top of the other. Could anyone tell me how to seperate the bars horizontally?

Also, how would I go about make a legend which will indicate sky_blue color as the control group and orange as the medicated group? Thanks!

2

2 Answers

0
votes

You can do it without loops: all orange bars in one go, then all sky-blue bars in one go. Note the aditional division by 2 in 'BarWidth'.

info = [mean1 mean2 mean3 mean4 mean5 mean6 mean7 nean8];
sky_blue = [86, 180, 233] / 256;
orange = [230, 159, 0] / 256

bar(1:2:numel(info), info(1:2:end), 'BarWidth', 0.5/2, 'Facecolor', orange);
hold on
bar(2:2:numel(info), info(2:2:end), 'BarWidth', 0.5/2, 'Facecolor', sky_blue);
0
votes

that has a quick solution! In each of the iteration, you are ploting anew bar plot without spedifiying its X coordinate, therefore all of them are ploted in X=1

Change yout code to (note the only change in bar() function) :

info = [mean1 mean2 mean3 mean4 mean5 mean6 mean7 nean8];
sky_blue = [86, 180, 233] / 256;
orange = [230, 159, 0] / 256

for d=1:length(info)
plot_bar(d)=bar(d,info(1,d), 'BarWidth', 0.5); 
    if mod(d,2)==0
       set(plot_bar(d),'FaceColor', sky_blue);
    else 
       set(plot_bar(d),'FaceColor', orange);
    end
hold on;
end

Output:

enter image description here

NOTE: you may probably want to use XtickLabel to label your plots, it seems it doesnt take it very well this method