Inconsistent behavior updating tick labeling

조회 수: 3 (최근 30일)
Leo Simon
Leo Simon 2018년 2월 13일
답변: Steven Lord 2018년 2월 13일
In the code below, I'm adding ticks to the Y axis. The first time I do it, YTickLabel updates correctly, adding an additional label. The second time I do it, YTickLabel fails to update properly, keeping the same number of elements when it should add one. I've played around with examples. In this case, the problem appears to be attributable to the fact that I'm adding a new tick below the first tick. If the second line of code is uncommented, then updating happens correctly. Is this a bug? Is there a workaround?
plot(0.5 :0.5:4.6)
%plot(0.0 :0.5:4.6)
YTick = get(gca,'YTick');
set(gca,'YTick',unique([ YTick , 1.9827 ]));
YTick = get(gca,'YTick');
YTickLabel = get(gca,'YTickLabel');
YTick
sprintf('YTick has %i elements', numel(YTick))
YTickLabel'
pause
set(gca,'YTick',unique([ YTick , 0.3519 ]));
YTick = get(gca,'YTick');
YTickLabel = get(gca,'YTickLabel');
YTick
sprintf('YTick has %i elements', numel(YTick))
YTickLabel'
pause

채택된 답변

Steven Lord
Steven Lord 2018년 2월 13일
If the axes property YTickLabelMode is set to 'auto' (which it is by default) and one of the values of the property YTick is outside the limits of the visible area of the axes returned by ylim, does the YTickLabel property contain a label for that non-visible tick? It does not. If you want that tick to be labeled, you can:
  1. explicitly set the YTickLabel property of the axes (directly or using the yticklabels function, if you're using release R2016b or later) which will change the YTickLabelMode as a side effect or
  2. change the limits of the axes using ylim so that all the ticks whose labels you want to see are visible

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Grid Lines, Tick Values, and Labels에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by