Problem with "grouped" bar plot

7 views (last 30 days)
Patrick Laux
Patrick Laux on 30 Jul 2021
Edited: dpb on 30 Jul 2021
Hi all,
I am using the following code to produce a "grouped" bar plot (4 groups and 2 subgroups, respectively). Now, I would like to use 8 different colors (1 for each bar) because the 2 subgroups are not the same for each group. I am confused why only two different colors are applied here. Actually, this is not a real grouped bar plot since I am not using b = bar(model_series, 'grouped'), isn't it?
I hope the problem became clear. Any help appreciated.
figure1 = figure;
axes1 = axes('Parent',figure1);
hold(axes1,'on');
b = bar(model_series,'Parent',axes1);
hold on
% Find the number of groups and the number of bars in each group
[ngroups, nbars] = size(model_series);
% Calculate the width for each bar group
groupwidth = min(0.8, nbars/(nbars + 1.5));
for i = 1:nbars
% Calculate center of each bar
x = (1:ngroups) - groupwidth/2 + (2*i-1) * groupwidth / (2*nbars);
errorbar(x, model_series(:,i), model_error(:,i), 'k', 'linestyle', 'none');
end
box(axes1,'on');
% Set the remaining axes properties
set(axes1,'XTick',[1 2 3 4],'XTickLabel',...
{'CU','MU','PU','TU'});
ylabel('Precipitation [mm/day]')
set(gca,'FontSize',16)
legend('1','2','3','4','5','6','7','8','orientation','vertical','location','NorthEastOutside')
hold off

Answers (1)

dpb
dpb on 30 Jul 2021
"... this is not a real grouped bar plot since I am not using b = bar(model_series, 'grouped'), isn't it?"
Au contraire, the documentation says
"bar(y) creates a bar graph with one bar for each element in y. If y is an m-by-n matrix, then bar creates m groups of n bars."
and your data are 4x2. Hence you have a grouped bar plot with two groups and therefore, two bar objects and two bar colors.
NB: the 'XEndPoints' property is in the bar handle now to be able to use for the X positions needed so can write as
hB=bar(model_series);
hold on
hE=arrayfun(@(h,i)errorbar(h.XEndPoints,h.YEndPoints,model_error(:,i),'k','linestyle','none'),hB,1:2);
To change the bar colors, have to use subterfuge;
set(hB,{'FaceColor'},'{'Flat'})
and then set the .CData color triplet data for each bar as wanted -- each of the four bars in each group is a line in the .CData array. There is an example in the documentation for bar
  3 Comments
Patrick Laux
Patrick Laux on 30 Jul 2021
Thank you very much for your answer. I will try your suggestions.
Concerning the other comment: you are right that there is no negative precipitation, however, what is shown is actually the mean and the standard deviation of an area. Standard deviation can of course exceed the mean values ;-)
dpb
dpb on 30 Jul 2021
Edited: dpb on 30 Jul 2021
There still is no negative rainfaill...the standard deviation is just a number; the distribution of rainfall is not even approximately normally distributed excepting over larger areas and times and may not be even then for many regions.
You can still compute the various statistics and they have some conceptual meaning, granted, but Ii's just not meaningful to present negative rainfall numbers regardless of what the computed statistics may be/produce.
Box plot or the like might be more suitable for these data than simple bar graph.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!