how to use the "tight" function for just one axis

So I origionally had: axis tight But it cut both axes, whereas I just want it to be tight around the x axis but allow the y axis to not be cut. Is there a function for this please? Thanks

 채택된 답변

dpb
dpb 2016년 7월 14일

1 개 추천

Not at hand, but it's not difficult to do...
function ytight(ax)
% Set axis tight only on y-axes
yl=xlim(ax); % retrieve auto y-limits
axis tight % set tight range
ylim(ax,yl) % restore y limits
end
You can also actually compute min, max of X-axis data and just set it, which is what is done
xlim([min(x) max(x)])

댓글 수: 4

Thanks, I'll give that a go, but which variable does ax represent please? Thanks!
I did it the second way you said and it worked, thanks!
dpb
dpb 2016년 7월 15일
"which variable does ax represent?"
The axes handle in question...typically the result of gca, but having it as an argument to the function lets the function be used in general, not just on the current axes. "Mo' bettah!" would be to make it optional and default to gca, but that was more coding effort than wanted to go to at the moment...
xlim can actually do this without manually setting the limits:
xlim('tight')
which I have added as a seperate answer below.

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

추가 답변 (3개)

Jaromir
Jaromir 2018년 3월 16일

9 개 추천

There's an out-of-the-box command for what you want:
set(handleToAxes, 'XLimSpec', 'Tight');
or
set(handleToAxes, 'YLimSpec', 'Tight');

댓글 수: 3

I found it didn't work with "handleToAxes" but it did with "gca" instead. Thank you! :)
Undocumented properties! But useful. How many undocumented properties are there?
dpb
dpb 2022년 4월 26일
Undocumented...

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

Drew Chap
Drew Chap 2023년 5월 25일

0 개 추천

As of R2021a, the best method to do this is:
ax = gca()
ax.XLimitMethod = 'tight'

카테고리

도움말 센터File Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

태그

질문:

2016년 7월 14일

답변:

2023년 5월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by