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

421 visualizaciones (últimos 30 días)
Jamie Shelley
Jamie Shelley el 14 de Jul. de 2016
Respondida: Drew Chap el 25 de Mayo de 2023
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

Respuesta aceptada

dpb
dpb el 14 de Jul. de 2016
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 comentarios
dpb
dpb el 15 de Jul. de 2016
"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
ADSW121365 el 31 de Ag. de 2022
xlim can actually do this without manually setting the limits:
xlim('tight')
which I have added as a seperate answer below.

Iniciar sesión para comentar.

Más respuestas (3)

Jaromir
Jaromir el 16 de Mzo. de 2018
There's an out-of-the-box command for what you want:
set(handleToAxes, 'XLimSpec', 'Tight');
or
set(handleToAxes, 'YLimSpec', 'Tight');
  3 comentarios
qilin guo
qilin guo el 26 de Abr. de 2022
Undocumented properties! But useful. How many undocumented properties are there?
dpb
dpb el 26 de Abr. de 2022
Undocumented...

Iniciar sesión para comentar.


ADSW121365
ADSW121365 el 31 de Ag. de 2022

Drew Chap
Drew Chap el 25 de Mayo de 2023
As of R2021a, the best method to do this is:
ax = gca()
ax.XLimitMethod = 'tight'

Categorías

Más información sobre Graphics Object Properties en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by