X Bar doesn't show all labels

조회 수: 31 (최근 30일)
S. B.
S. B. 2020년 9월 16일
댓글: S. B. 2020년 9월 16일
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

채택된 답변

Rik
Rik 2020년 9월 16일
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
Rik
Rik 2020년 9월 16일
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. 2020년 9월 16일
That works perfectly! Thanks!!

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2020년 9월 16일
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.

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by