Too many points on x-axis

조회 수: 19 (최근 30일)
Karl
Karl 2013년 6월 4일
If you run the following script, Matlab uses 0.5 increments on the x-axis. This gives the wrong label, since it starts over again after 6 points on the x-axis. Does anybody know how to make sure that the increments are made so that the labels get correct?
test= rand(11,8,6);
test2=test
Alder = {'<20', '2029','3039','4049','5059','6069','>70', 'all'};
Aar = {'2011', '2012', '2013_1', '2014_1', '2014_s2', '2014_s5'};
nAlder = length(Alder);
nAar = length(Aar);
figure
hold on
for iAlder = 1:nAlder
plot(squeeze(test2(11,iAlder,:)));
set(gca, 'XTickLabel',Aar)
end
hold off

답변 (2개)

Karl
Karl 2013년 6월 5일
편집: Karl 2013년 6월 5일
Putting the following line between the plot-command line and the XTickLabel-command worked
set(gca, 'XTick',[1 2 3 4 5 6])
Does anybody know why Matlab uses 0.5 increments on the axis? Is this always a problem, so that one always has to use the extra command line listed above in cases like mine?
  댓글 수: 2
Iain
Iain 2013년 6월 5일
Matlab just runs through a set process for determining the tick values. If you have more than 6 x values, you'll see the tick locations change.
If you want specific tick locations, you need to specify them.
Karl
Karl 2013년 6월 5일
The funny thing is that there are only 6 values originally...

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


Jan
Jan 2013년 6월 5일
Perhaps this helps:
figure
data = squeeze(test2(11, :, :));
plot(1:length(Alder), data); % perhaps: data.'
set(gca, 'XTickLabel', Aar, 'XTick', 1:length(Alder))
  댓글 수: 2
Karl
Karl 2013년 6월 5일
The same problem appears when running your commands.
I think this illustrates the source of the problem:
test= rand(11,8,6);
figure
hold on
for i= 1:8
plot(squeeze(test(11,i,:)));
end
hold off
Try running the commands above. You will see that there are 11 x-axis "labels/points" with 0.5 increments, dispite that there are only y-vaules for 6 of these points. I think this is the source of the problem. When one uses XThickLabel and a 1x6 vector, it will be repeated after the 6th point/label in the original plot. The question is why Matlab does not make 6 points in the first place. Has it something to do with the test-matrix having 11 rows?
Iain
Iain 2013년 6월 5일
No. It simply comes up with what matlab reckons the best scaling is for the plot. It seems to try to ensure that there are BETWEEN 7 and 11 ticks.
Try:
for i = 1:30
figure
plot(randn(i,1))
end
You'll see what I mean.

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

카테고리

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