X Bar doesn't show all labels
Show older comments
Hi,
I was using the code of the following thread, but on the x asis only values 1 and 2 are shown as labels, although there are 11 bars and I added all labels with:
set(gca,'XTickLabel',{'1','2','3','4','5','6','7','8','9','10','11'}, 'fontSize', 24)
Can anyone help?
Link to thread: https://de.mathworks.com/matlabcentral/answers/354435-changing-color-of-individual-parts-of-stacked-bars
This is the code:
data =[
8.5000 2.5000 25.0000 NaN NaN NaN NaN
3.0000 28.0000 32.0000 2.0000 NaN NaN NaN
23.0000 NaN NaN NaN NaN NaN NaN
3.0000 2.5000 4.5000 5.0000 9.5000 3.0000 2.5000
6.0000 21.0000 7.0000 NaN NaN NaN NaN
7.5000 34.5000 NaN NaN NaN NaN NaN
3.0000 6.0000 24.0000 NaN NaN NaN NaN
2.0000 22.0000 NaN NaN NaN NaN NaN
5.0000 37.0000 NaN NaN NaN NaN NaN
1.5000 5.5000 25.0000 NaN NaN NaN NaN
1.0000 1.0000 1.5000 23.5000 NaN NaN NaN];
data_class={
'g' 'gc' 'c' '' '' '' ''
'g' 'gc' 'c' 'gc' '' '' ''
'c' '' '' '' '' '' ''
'g' 'c' 'g' 'gc' 'c' 'g' 'c'
'g' 'gc' 'c' '' '' '' ''
'g' 'c' '' '' '' '' ''
'g' 'gc' 'c' '' '' '' ''
'g' 'c' '' '' '' '' ''
'g' 'c' '' '' '' '' ''
'gc' 'g' 'c' '' '' '' ''
'g' 'gc' 'g' 'c' '' '' '' };
figure(1),clf(1)
data(end+1,:)=NaN;
b={bar([1 2],data([1 end],:),'stacked')};
hold on
for k=2:(size(data,1)-1)
b{k}=bar([k-1 k],data([end k],:),'stacked');
end
for k=1:(size(data,1)-1)
for k2=1:size(data,2)
switch data_class{k,k2}
case 'g'
set(b{k}(k2),'FaceColor','g')
case 'c'
set(b{k}(k2),'FaceColor','b')
case 'gc'
set(b{k}(k2),'FaceColor','c')
end
end
end

Accepted Answer
More Answers (1)
Walter Roberson
on 16 Sep 2020
0 votes
I tracked through the code for bar(). It only puts in box labels when "hold" is off.
It would work better if you were to bar() all of the data in one call.
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!