MATLAB Answers

Problem with "grouped" bar plot

조회 수: 8(최근 30일)
Patrick Laux
Patrick Laux 2021년 7월 30일
편집: dpb 2021년 7월 30일
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

답변(1개)

dpb
dpb 2021년 7월 30일
"... 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
dpb
dpb 2021년 7월 30일
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.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by