1 | function ax = axis_pct(pct)
|
---|
2 | % AXIS_PCT Set reasonable axis limits.
|
---|
3 | %
|
---|
4 | % AXIS_PCT(pct) sets axis limits to extend pct% beyond limits of plotted
|
---|
5 | % objects. Default is 5%.
|
---|
6 | % Works for linear or log scale.
|
---|
7 | % Unfortunately, the axes won't change when new points are plotted.
|
---|
8 |
|
---|
9 | % Written by Tom Minka
|
---|
10 |
|
---|
11 | if nargin < 1
|
---|
12 | pct = 0.05;
|
---|
13 | end
|
---|
14 | ax = [Inf -Inf Inf -Inf Inf -Inf];
|
---|
15 |
|
---|
16 | % find bounding box of plotted objects
|
---|
17 | children = get(gca,'children');
|
---|
18 | for child = children'
|
---|
19 | if strcmp(get(child,'type'),'text')
|
---|
20 | xyz = get(child,'position');
|
---|
21 | % need to determine bounding box of the text
|
---|
22 | if strcmp(get(gca,'xscale'), 'log')
|
---|
23 | xyz(1) = log10(xyz(1));
|
---|
24 | end
|
---|
25 | if strcmp(get(gca,'yscale'), 'log')
|
---|
26 | xyz(2) = log10(xyz(2));
|
---|
27 | end
|
---|
28 | if strcmp(get(gca,'zscale'), 'log')
|
---|
29 | xyz(3) = log10(xyz(3));
|
---|
30 | end
|
---|
31 | c([1 2]) = xyz(1);
|
---|
32 | c([3 4]) = xyz(2);
|
---|
33 | c([5 6]) = xyz(3);
|
---|
34 | else
|
---|
35 | x = get(child,'xdata');
|
---|
36 | x = x(finite(x));
|
---|
37 | if strcmp(get(gca,'xscale'), 'log')
|
---|
38 | x = x(x > 0);
|
---|
39 | x = log10(x);
|
---|
40 | end
|
---|
41 | if isempty(x)
|
---|
42 | c([1 2]) = 0;
|
---|
43 | else
|
---|
44 | c([1 2]) = [min(x(:)) max(x(:))];
|
---|
45 | end
|
---|
46 | y = get(child,'ydata');
|
---|
47 | y = y(finite(y));
|
---|
48 | if strcmp(get(gca,'yscale'), 'log')
|
---|
49 | y = y(y > 0);
|
---|
50 | y = log10(y);
|
---|
51 | end
|
---|
52 | if isempty(y)
|
---|
53 | c([3 4]) = 0;
|
---|
54 | else
|
---|
55 | c([3 4]) = [min(y(:)) max(y(:))];
|
---|
56 | end
|
---|
57 | try
|
---|
58 | z = get(child,'zdata');
|
---|
59 | z = z(finite(z));
|
---|
60 | if isempty(z)
|
---|
61 | c([5 6]) = 0;
|
---|
62 | else
|
---|
63 | if strcmp(get(gca,'zscale'), 'log')
|
---|
64 | z = z(z > 0);
|
---|
65 | z = log10(z);
|
---|
66 | end
|
---|
67 | c([5 6]) = [min(z(:)) max(z(:))];
|
---|
68 | end
|
---|
69 | end
|
---|
70 | end
|
---|
71 | ax([1 3 5]) = min(ax([1 3 5]), c([1 3 5]));
|
---|
72 | ax([2 4 6]) = max(ax([2 4 6]), c([2 4 6]));
|
---|
73 | end
|
---|
74 | dx = ax(2)-ax(1);
|
---|
75 | if dx == 0
|
---|
76 | dx = 1;
|
---|
77 | end
|
---|
78 | dy = ax(4)-ax(3);
|
---|
79 | if dy == 0
|
---|
80 | dy = 1;
|
---|
81 | end
|
---|
82 | dz = ax(6)-ax(5);
|
---|
83 | if dz == 0
|
---|
84 | dz = 1;
|
---|
85 | end
|
---|
86 | ax = ax + [-dx dx -dy dy -dz dz]*pct;
|
---|
87 | if strcmp(get(gca,'xscale'), 'log')
|
---|
88 | ax([1 2]) = 10.^(ax([1 2]));
|
---|
89 | end
|
---|
90 | if strcmp(get(gca,'yscale'), 'log')
|
---|
91 | ax([3 4]) = 10.^(ax([3 4]));
|
---|
92 | end
|
---|
93 | if strcmp(get(gca,'zscale'), 'log')
|
---|
94 | ax([5 6]) = 10.^(ax([5 6]));
|
---|
95 | end
|
---|
96 | % clip for 2D
|
---|
97 | ax = ax(1:length(axis));
|
---|
98 | if ~isempty(children)
|
---|
99 | axis(ax);
|
---|
100 | end
|
---|
101 | if nargout < 1
|
---|
102 | clear ax
|
---|
103 | end
|
---|