how to use the "tight" function for just one axis
288 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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
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 个评论
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:
xlim('tight')
which I have added as a seperate answer below.
更多回答(3 个)
Drew Chap
2023-5-25
As of R2021a, the best method to do this is:
ax = gca()
ax.XLimitMethod = 'tight'
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!