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

429 ビュー (過去 30 日間)
Jamie Shelley
Jamie Shelley 2016 年 7 月 14 日
回答済み: Drew Chap 2023 年 5 月 25 日
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 2016 年 7 月 14 日
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
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 件のコメント
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...
ADSW121365 2022 年 8 月 31 日
xlim can actually do this without manually setting the limits:
which I have added as a seperate answer below.


その他の回答 (3 件)

Jaromir 2018 年 3 月 16 日
There's an out-of-the-box command for what you want:
set(handleToAxes, 'XLimSpec', 'Tight');
set(handleToAxes, 'YLimSpec', 'Tight');
  3 件のコメント
qilin guo
qilin guo 2022 年 4 月 26 日
Undocumented properties! But useful. How many undocumented properties are there?
dpb 2022 年 4 月 26 日


ADSW121365 2022 年 8 月 31 日
Documentated Function:

Drew Chap
Drew Chap 2023 年 5 月 25 日
As of R2021a, the best method to do this is:
ax = gca()
ax.XLimitMethod = 'tight'


Help Center および File ExchangeAxes Appearance についてさらに検索


Community Treasure Hunt

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

Start Hunting!

Translated by