1
$\begingroup$

How can I automatically set the limits of my axis to be e.g. 5% bigger than all data contained in the plot? Of course I could calculate the total max/min values for all the stuff I plot but I would have to do this for each plot separately. Since Matlab somehow must know all min/max values (it computes a decent limit itself) I guess it must be easier to do this.

$\endgroup$
4
$\begingroup$

Something like the following should do what you want:

p=0.05;
axis tight
a=axis();
new_limits=[a(1)-p*(a(2)-a(1)) a(2)+p*(a(2)-a(1)) a(3)-p*(a(4)-a(3)) a(4)+p*(a(4)-a(3))];
axis(new_limits);

Further refinement would be to make this into a function that accepts any figure handle and allows a different expansion in $x$ and $y$, but those should be straightforward.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.