Group bar in histogram, add label

5 views (last 30 days)
MiauMiau
MiauMiau on 8 Mar 2017
Answered: Fernando Bello on 3 Apr 2019
Hi,
I have the following code for my histograms already:
mean_accuracy = [0.37,0.44,0.67,0.39,0.64,0.64]; % mean accuracy
std_accuracy = [0.15,0.07,0.07,0.09,0.04,0.03]; % standard deviation of accuracy
figure
hold on
bar(1:6,mean_accuracy)
errorbar(1:6,mean_accuracy,std_accuracy,'.')
axis([ 0 8 0 1])
set(gca,'xtick',[1 2 3 4 5 6 ])
set(gca,'xticklabel',{'2', '3', '4', '3', '4', '4'})
What I want to do, is group the bars as follows: The three first belong together, then the following two, and then the last one (so three groups). Each of these groups should have a label (ie Condition 1, Condition 2, Condition 3), and the groups should have some distance to each other on the x-axis. How can I do that in a simple way - I could not figure due to my inclusion of error bars.. Many thanks

Answers (1)

Fernando Bello
Fernando Bello on 3 Apr 2019
Did you solve this problem?
Im trying to do something similar.
You can write your data as a matrix, where each col is a group.
For example: for two groups and 5 categories.
My data is number of car for 5 diferent colors at 2 different brands.
Lets say we have
Brand 1 brand 2
red 5 3
blue 4 1
green 3 4
yellow 1 3
orange 0 5
you can write that as a matrix
Data = [5,3;
4, 1;
3, 4;
1, 3;
0, 5];
then use
b = bar(Data)
and if you want to modify the colors of the bars... this will do the job
b(1).FaceColor = 'b'
b(2).FaceColor = 'r'
Then you can place your thicks
xticklabels ({'red','blue','green','yellow','orange'})
and thats it!
Best regards,
F.Bello

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!