matthiasm@8: function ax = axis_pct(pct) matthiasm@8: % AXIS_PCT Set reasonable axis limits. matthiasm@8: % AXIS_PCT(pct) sets axis limits to extend pct% beyond limits of plotted matthiasm@8: % objects. Default is 5%. matthiasm@8: % Works for linear or log scale. matthiasm@8: % Unfortunately, the axes won't change when new points are plotted. matthiasm@8: matthiasm@8: if nargin < 1 matthiasm@8: pct = 0.05; matthiasm@8: end matthiasm@8: ax = [Inf -Inf Inf -Inf Inf -Inf]; matthiasm@8: matthiasm@8: % find bounding box of plotted objects matthiasm@8: children = get(gca,'children'); matthiasm@8: for child = children' matthiasm@8: if strcmp(get(child,'type'),'text') matthiasm@8: xyz = get(child,'position'); matthiasm@8: % need to determine bounding box of the text matthiasm@8: c([1 2]) = xyz(1); matthiasm@8: c([3 4]) = xyz(2); matthiasm@8: c([5 6]) = xyz(3); matthiasm@8: else matthiasm@8: x = get(child,'xdata'); matthiasm@8: c(1) = min(x); matthiasm@8: c(2) = max(x); matthiasm@8: y = get(child,'ydata'); matthiasm@8: c(3) = min(y); matthiasm@8: c(4) = max(y); matthiasm@8: z = get(child,'zdata'); matthiasm@8: if isempty(z) matthiasm@8: c([5 6]) = 0; matthiasm@8: else matthiasm@8: c(5) = min(z); matthiasm@8: c(6) = max(z); matthiasm@8: end matthiasm@8: end matthiasm@8: ax([1 3 5]) = min(ax([1 3 5]), c([1 3 5])); matthiasm@8: ax([2 4 6]) = max(ax([2 4 6]), c([2 4 6])); matthiasm@8: end matthiasm@8: if strcmp(get(gca,'xscale'), 'log') matthiasm@8: ax([1 2]) = log(ax([1 2])); matthiasm@8: end matthiasm@8: if strcmp(get(gca,'yscale'), 'log') matthiasm@8: ax([3 4]) = log(ax([3 4])); matthiasm@8: end matthiasm@8: dx = ax(2)-ax(1); matthiasm@8: if dx == 0 matthiasm@8: dx = 1; matthiasm@8: end matthiasm@8: dy = ax(4)-ax(3); matthiasm@8: if dy == 0 matthiasm@8: dy = 1; matthiasm@8: end matthiasm@8: dz = ax(6)-ax(5); matthiasm@8: if dz == 0 matthiasm@8: dz = 1; matthiasm@8: end matthiasm@8: ax = ax + [-dx dx -dy dy -dz dz]*pct; matthiasm@8: if strcmp(get(gca,'xscale'), 'log') matthiasm@8: ax([1 2]) = exp(ax([1 2])); matthiasm@8: end matthiasm@8: if strcmp(get(gca,'yscale'), 'log') matthiasm@8: ax([3 4]) = exp(ax([3 4])); matthiasm@8: end matthiasm@8: % clip for 2D matthiasm@8: ax = ax(1:length(axis)); matthiasm@8: axis(ax); matthiasm@8: if nargout < 1 matthiasm@8: clear ax matthiasm@8: end