X Bar doesn't show all labels

28 ビュー (過去 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 Center および File ExchangeAxis Labels についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by