7017
|
1 ## Copyright (C) 1994, 1995, 1996, 1997, 1999, 2000, 2002, 2003, 2004, |
|
2 ## 2005, 2006, 2007 John W. Eaton |
2313
|
3 ## |
|
4 ## This file is part of Octave. |
|
5 ## |
|
6 ## Octave is free software; you can redistribute it and/or modify it |
|
7 ## under the terms of the GNU General Public License as published by |
7016
|
8 ## the Free Software Foundation; either version 3 of the License, or (at |
|
9 ## your option) any later version. |
2313
|
10 ## |
|
11 ## Octave is distributed in the hope that it will be useful, but |
|
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 ## General Public License for more details. |
|
15 ## |
|
16 ## You should have received a copy of the GNU General Public License |
7016
|
17 ## along with Octave; see the file COPYING. If not, see |
|
18 ## <http://www.gnu.org/licenses/>. |
590
|
19 |
3368
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} axis (@var{limits}) |
3667
|
22 ## Set axis limits for plots. |
3426
|
23 ## |
3368
|
24 ## The argument @var{limits} should be a 2, 4, or 6 element vector. The |
|
25 ## first and second elements specify the lower and upper limits for the x |
|
26 ## axis. The third and fourth specify the limits for the y axis, and the |
|
27 ## fifth and sixth specify the limits for the z axis. |
3426
|
28 ## |
4945
|
29 ## Without any arguments, @code{axis} turns autoscaling on. |
|
30 ## |
|
31 ## With one output argument, @code{x=axis} returns the current axes |
3667
|
32 ## |
3668
|
33 ## The vector argument specifying limits is optional, and additional |
|
34 ## string arguments may be used to specify various axis properties. For |
3667
|
35 ## example, |
|
36 ## |
|
37 ## @example |
|
38 ## axis ([1, 2, 3, 4], "square"); |
|
39 ## @end example |
|
40 ## |
|
41 ## @noindent |
3668
|
42 ## forces a square aspect ratio, and |
|
43 ## |
|
44 ## @example |
|
45 ## axis ("labely", "tic"); |
|
46 ## @end example |
|
47 ## |
|
48 ## @noindent |
|
49 ## turns tic marks on for all axes and tic mark labels on for the y-axis |
|
50 ## only. |
3667
|
51 ## |
|
52 ## @noindent |
|
53 ## The following options control the aspect ratio of the axes. |
|
54 ## |
|
55 ## @table @code |
|
56 ## @item "square" |
|
57 ## Force a square aspect ratio. |
|
58 ## @item "equal" |
|
59 ## Force x distance to equal y-distance. |
|
60 ## @item "normal" |
|
61 ## Restore the balance. |
|
62 ## @end table |
|
63 ## |
|
64 ## @noindent |
|
65 ## The following options control the way axis limits are interpreted. |
|
66 ## |
|
67 ## @table @code |
|
68 ## @item "auto" |
|
69 ## Set the specified axes to have nice limits around the data |
|
70 ## or all if no axes are specified. |
|
71 ## @item "manual" |
|
72 ## Fix the current axes limits. |
|
73 ## @item "tight" |
|
74 ## Fix axes to the limits of the data (not implemented). |
|
75 ## @end table |
|
76 ## |
|
77 ## @noindent |
|
78 ## The option @code{"image"} is equivalent to @code{"tight"} and |
|
79 ## @code{"equal"}. |
|
80 ## |
|
81 ## @noindent |
|
82 ## The following options affect the appearance of tic marks. |
|
83 ## |
|
84 ## @table @code |
|
85 ## @item "on" |
|
86 ## Turn tic marks and labels on for all axes. |
|
87 ## @item "off" |
|
88 ## Turn tic marks off for all axes. |
|
89 ## @item "tic[xyz]" |
3668
|
90 ## Turn tic marks on for all axes, or turn them on for the |
|
91 ## specified axes and off for the remainder. |
3667
|
92 ## @item "label[xyz]" |
3668
|
93 ## Turn tic labels on for all axes, or turn them on for the |
|
94 ## specified axes and off for the remainder. |
3667
|
95 ## @item "nolabel" |
|
96 ## Turn tic labels off for all axes. |
|
97 ## @end table |
|
98 ## Note, if there are no tic marks for an axis, there can be no labels. |
|
99 ## |
|
100 ## @noindent |
|
101 ## The following options affect the direction of increasing values on |
|
102 ## the axes. |
|
103 ## |
|
104 ## @table @code |
|
105 ## @item "ij" |
|
106 ## Reverse y-axis, so lower values are nearer the top. |
|
107 ## @item "xy" |
|
108 ## Restore y-axis, so higher values are nearer the top. |
|
109 ## @end table |
4945
|
110 ## |
7189
|
111 ## If an axes handle is passed as the first argument, then operate on |
|
112 ## this axes rather than the current axes. |
3368
|
113 ## @end deftypefn |
590
|
114 |
2314
|
115 ## Author: jwe |
|
116 |
5561
|
117 ## PKG_ADD: mark_as_command axis |
5560
|
118 |
7189
|
119 function varargout = axis (varargin) |
590
|
120 |
7189
|
121 if (nargin > 0 && isscalar (varargin{1}) && ishandle (varargin{1})) |
|
122 h = varargin{1}; |
|
123 if (! strcmp (get (h, "type"), "axes")) |
|
124 error ("axis: expecting first argument to be an axes object"); |
|
125 endif |
|
126 oldh = gca (); |
|
127 unwind_protect |
|
128 axes (h); |
|
129 varargout = cell (max (nargin == 1, nargout), 1); |
|
130 if (isempty (varargout)) |
|
131 __axis__ (h, varargin{2:end}); |
|
132 else |
|
133 [varargout{:}] = __axis__ (h, varargin{2:end}); |
|
134 endif |
|
135 unwind_protect_cleanup |
|
136 axes (oldh); |
|
137 end_unwind_protect |
|
138 else |
|
139 varargout = cell (max (nargin == 0, nargout), 1); |
|
140 if (isempty (varargout)) |
|
141 __axis__ (gca (), varargin{:}); |
|
142 else |
|
143 [varargout{:}] = __axis__ (gca (), varargin{:}); |
|
144 endif |
|
145 endif |
590
|
146 |
7189
|
147 endfunction |
|
148 |
|
149 function curr_axis = __axis__ (ca, ax, varargin) |
|
150 |
|
151 if (nargin == 1) |
4945
|
152 if (nargout == 0) |
6257
|
153 set (ca, "xlimmode", "auto", "ylimmode", "auto", "zlimmode", "auto"); |
4945
|
154 else |
6257
|
155 xlim = get (ca, "xlim"); |
|
156 ylim = get (ca, "ylim"); |
|
157 zlim = get (ca, "zlim"); |
|
158 curr_axis = [xlim, ylim, zlim]; |
4945
|
159 endif |
3667
|
160 |
5443
|
161 elseif (ischar (ax)) |
3667
|
162 ax = tolower (ax); |
4340
|
163 len = length (ax); |
3667
|
164 |
|
165 ## 'matrix mode' to reverse the y-axis |
|
166 if (strcmp (ax, "ij")) |
6257
|
167 set (ca, "ydir", "reverse"); |
3667
|
168 elseif (strcmp (ax, "xy")) |
6257
|
169 set (ca, "ydir", "normal"); |
3667
|
170 |
|
171 ## aspect ratio |
|
172 elseif (strcmp (ax, "image")) |
6257
|
173 set (ca, "dataaspectratio", [1, 1, 1]); |
|
174 ## FIXME should be the same as "tight" |
|
175 set (ca, "xlimmode", "auto", "ylimmode", "auto", "zlimmode", "auto"); |
|
176 elseif (strcmp (ax, "equal") || strcmp (ax, "square")) |
|
177 set (ca, "dataaspectratio", [1, 1, 1]); |
3667
|
178 elseif (strcmp (ax, "normal")) |
6257
|
179 set (ca, "dataaspectratiomode", "auto"); |
3667
|
180 |
|
181 ## axis limits |
4340
|
182 elseif (len >= 4 && strcmp (ax(1:4), "auto")) |
|
183 if (len > 4) |
6257
|
184 if (any (ax == "x")) |
|
185 set (ca, "xlimmode", "auto"); |
|
186 endif |
|
187 if (any (ax == "y")) |
|
188 set (ca, "ylimmode", "auto"); |
|
189 endif |
|
190 if (any (ax == "z")) |
|
191 set (ca, "zlimmode", "auto"); |
|
192 endif |
3667
|
193 else |
6257
|
194 set (ca, "xlimmode", "auto", "ylimmode", "auto", "zlimmode", "auto"); |
3667
|
195 endif |
|
196 elseif (strcmp (ax, "manual")) |
|
197 ## fixes the axis limits, like axis(axis) should; |
6257
|
198 set (ca, "xlimmode", "manual", "ylimmode", "manual", "zlimmode", "manual"); |
3667
|
199 elseif (strcmp (ax, "tight")) |
5775
|
200 ## FIXME if tight, plot must set ranges to limits of the |
3667
|
201 ## all the data on the current plot, even if from a previous call. |
|
202 ## Instead, just let gnuplot do as it likes. |
6257
|
203 set (ca, "xlimmode", "auto", "ylimmode", "auto", "zlimmode", "auto"); |
3667
|
204 |
|
205 ## tic marks |
6257
|
206 elseif (strcmp (ax, "on") || strcmp (ax, "tic")) |
|
207 set (ca, "xtickmode", "auto", "ytickmode", "auto", "ztickmode", "auto"); |
|
208 set (ca, "xticklabelmode", "auto", "yticklabelmode", "auto", |
|
209 "zticklabelmode", "auto"); |
6765
|
210 set (ca, "visible", "on"); |
3667
|
211 elseif (strcmp (ax, "off")) |
6257
|
212 set (ca, "xtick", [], "ytick", [], "ztick", []); |
6765
|
213 set (ca, "visible", "off"); |
4340
|
214 elseif (len > 3 && strcmp (ax(1:3), "tic")) |
|
215 if (any (ax == "x")) |
6257
|
216 set (ca, "xtickmode", "auto"); |
3667
|
217 else |
6257
|
218 set (ca, "xtick", []); |
3667
|
219 endif |
4340
|
220 if (any (ax == "y")) |
6257
|
221 set (ca, "ytickmode", "auto"); |
3667
|
222 else |
6257
|
223 set (ca, "ytick", []); |
3667
|
224 endif |
4340
|
225 if (any (ax == "z")) |
6257
|
226 set (ca, "ztickmode", "auto"); |
3667
|
227 else |
6257
|
228 set (ca, "ztick", []); |
3667
|
229 endif |
|
230 elseif (strcmp (ax, "label")) |
6257
|
231 set (ca, "xticklabelmode", "auto", "yticklabelmode", "auto", |
|
232 "zticklabelmode", "auto"); |
3667
|
233 elseif (strcmp (ax, "nolabel")) |
6257
|
234 set (ca, "xticklabel", "", "yticklabel", "", "zticklabel", ""); |
4340
|
235 elseif (len > 5 && strcmp (ax(1:5), "label")) |
|
236 if (any (ax == "x")) |
6257
|
237 set (ca, "xticklabelmode", "auto"); |
3667
|
238 else |
6257
|
239 set (ca, "xticklabel", ""); |
3667
|
240 endif |
4340
|
241 if (any (ax == "y")) |
6257
|
242 set (ca, "yticklabelmode", "auto"); |
3667
|
243 else |
6257
|
244 set (ca, "yticklabel", ""); |
3667
|
245 endif |
4340
|
246 if (any (ax == "z")) |
6257
|
247 set (ca, "zticklabelmode", "auto"); |
3667
|
248 else |
6257
|
249 set (ca, "zticklabel", ""); |
3667
|
250 endif |
|
251 |
|
252 else |
4259
|
253 warning ("unknown axis option '%s'", ax); |
3667
|
254 endif |
|
255 |
4030
|
256 elseif (isvector (ax)) |
590
|
257 |
|
258 len = length (ax); |
|
259 |
|
260 if (len != 2 && len != 4 && len != 6) |
|
261 error ("axis: expecting vector with 2, 4, or 6 elements"); |
|
262 endif |
|
263 |
5627
|
264 for i = 1:2:len |
|
265 if (ax(i) == ax(i+1)) |
|
266 error ("axis: limits(%d) cannot equal limits(%d)", i, i+1); |
|
267 endif |
|
268 endfor |
|
269 |
590
|
270 if (len > 1) |
6257
|
271 set (ca, "xlim", [ax(1), ax(2)]); |
590
|
272 endif |
|
273 |
|
274 if (len > 3) |
6257
|
275 set (ca, "ylim", [ax(3), ax(4)]); |
590
|
276 endif |
|
277 |
|
278 if (len > 5) |
6257
|
279 set (ca, "zlim", [ax(5), ax(6)]); |
590
|
280 endif |
|
281 |
|
282 else |
595
|
283 error ("axis: expecting no args, or a vector with 2, 4, or 6 elements"); |
590
|
284 endif |
|
285 |
7189
|
286 if (nargin > 2) |
|
287 __axis__ (ca, varargin{:}); |
3667
|
288 endif |
6447
|
289 |
590
|
290 endfunction |
3667
|
291 |
|
292 %!demo |
|
293 %! t=0:0.01:2*pi; x=sin(t); |
|
294 %! |
|
295 %! subplot(221); title("normal plot"); |
6746
|
296 %! plot(t, x); |
3667
|
297 %! |
|
298 %! subplot(222); title("square plot"); |
6746
|
299 %! axis("square"); plot(t, x); |
3667
|
300 %! |
|
301 %! subplot(223); title("equal plot"); |
6746
|
302 %! axis("equal"); plot(t, x); |
3667
|
303 %! |
|
304 %! subplot(224); title("normal plot again"); |
6746
|
305 %! axis("normal"); plot(t, x); |
3667
|
306 |
|
307 %!demo |
|
308 %! t=0:0.01:2*pi; x=sin(t); |
|
309 %! |
|
310 %! subplot(121); title("ij plot"); |
6746
|
311 %! axis("ij"); plot(t, x); |
3667
|
312 %! |
|
313 %! subplot(122); title("xy plot"); |
6746
|
314 %! axis("xy"); plot(t, x); |
3667
|
315 |
|
316 %!demo |
|
317 %! t=0:0.01:2*pi; x=sin(t); |
|
318 %! |
|
319 %! subplot(331); title("x tics & labels"); |
6746
|
320 %! axis("ticx"); plot(t, x); |
3667
|
321 %! |
|
322 %! subplot(332); title("y tics & labels"); |
6746
|
323 %! axis("ticy"); plot(t, x); |
3667
|
324 %! |
|
325 %! subplot(334); title("x & y tics, x labels"); |
6746
|
326 %! axis("labelx","tic"); plot(t, x); |
3667
|
327 %! |
|
328 %! subplot(335); title("x & y tics, y labels"); |
6746
|
329 %! axis("labely","tic"); plot(t, x); |
3667
|
330 %! |
|
331 %! subplot(337); title("x tics, no labels"); |
6746
|
332 %! axis("nolabel","ticx"); plot(t, x); |
3667
|
333 %! |
|
334 %! subplot(338); title("y tics, no labels"); |
6746
|
335 %! axis("nolabel","ticy"); plot(t, x); |
3667
|
336 %! |
|
337 %! subplot(333); title("no tics or labels"); |
6746
|
338 %! axis("off"); plot(t, x); |
3667
|
339 %! |
|
340 %! subplot(336); title("all tics but no labels"); |
6746
|
341 %! axis("nolabel","tic"); plot(t, x); |
3667
|
342 %! |
|
343 %! subplot(339); title("all tics & labels"); |
6746
|
344 %! axis("on"); plot(t, x); |
3667
|
345 |
|
346 %!demo |
|
347 %! t=0:0.01:2*pi; x=sin(t); |
|
348 %! |
|
349 %! subplot(321); title("axes at [0 3 0 1]") |
6746
|
350 %! axis([0,3,0,1]); plot(t, x); |
3667
|
351 %! |
|
352 %! subplot(322); title("auto"); |
6746
|
353 %! axis("auto"); plot(t, x); |
3667
|
354 %! |
|
355 %! subplot(323); title("manual"); |
|
356 %! plot(t, x, ";sine [0:2pi];"); hold on; |
|
357 %! axis("manual"); |
|
358 %! plot(-3:3,-3:3, ";line (-3,-3)->(3,3);"); hold off; |
|
359 %! |
|
360 %! subplot(324); title("axes at [0 3 0 1], then autox"); |
|
361 %! axis([0,3,0,1]); axis("autox"); |
|
362 %! plot(t, x, ";sine [0:2pi];"); |
|
363 %! |
|
364 %! subplot(325); title("axes at [3 6 0 1], then autoy"); |
|
365 %! axis([3,6,0,1]); axis("autoy"); |
|
366 %! plot(t, x, ";sine [0:2p];"); |
|
367 %! |
|
368 %! subplot(326); title("tight"); |
6746
|
369 %! axis("tight"); plot(t, x); |
3667
|
370 %! % The last plot should not have any whitespace outside the data |
|
371 %! % limits, but "tight" isn't implemented yet. |