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.


1 Answer 1


Something like the following should do what you want:

axis tight
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))];

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.


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