X Bar doesn't show all labels

28 views (last 30 days)
S. B.
S. B. on 16 Sep 2020
Commented: S. B. on 16 Sep 2020
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?
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

Rik
Rik on 16 Sep 2020
I don't really see why, but if you check get(gca,'XTick') you will notice that only the 1 and 2 have ticks in the first place.
%put original code here
set(gca,...
'XTick',1:size(data,1),...%fix the ticks
'fontSize', 24) %set the font size
  3 Comments
Rik
Rik on 16 Sep 2020
You're subtracting 1 from the data itself, not from the output of size:
set(gca,...
'XTick',1:(size(data,1)-1),...%fix the ticks
'fontSize', 24) %set the font size
S. B.
S. B. on 16 Sep 2020
That works perfectly! Thanks!!

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 16 Sep 2020
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.

Community Treasure Hunt

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

Start Hunting!