Mercurial > hg > octave-nkf
annotate scripts/plot/axis.m @ 8610:85c9906abfd1
use endif and endfor instead of end
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 27 Jan 2009 23:17:37 -0500 |
parents | cadc73247d65 |
children | 5dd06f19e9be |
rev | line source |
---|---|
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 ## | |
8507 | 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 |
7215 | 121 [h, varargin, nargin] = __plt_get_axis_arg__ ("axis", varargin{:}); |
7216 | 122 |
7215 | 123 oldh = gca (); |
124 unwind_protect | |
125 axes (h); | |
7189 | 126 varargout = cell (max (nargin == 0, nargout), 1); |
127 if (isempty (varargout)) | |
7215 | 128 __axis__ (h, varargin{:}); |
7189 | 129 else |
7215 | 130 [varargout{:}] = __axis__ (h, varargin{:}); |
7189 | 131 endif |
7215 | 132 unwind_protect_cleanup |
133 axes (oldh); | |
134 end_unwind_protect | |
590 | 135 |
7189 | 136 endfunction |
137 | |
138 function curr_axis = __axis__ (ca, ax, varargin) | |
139 | |
140 if (nargin == 1) | |
4945 | 141 if (nargout == 0) |
6257 | 142 set (ca, "xlimmode", "auto", "ylimmode", "auto", "zlimmode", "auto"); |
4945 | 143 else |
6257 | 144 xlim = get (ca, "xlim"); |
145 ylim = get (ca, "ylim"); | |
146 zlim = get (ca, "zlim"); | |
147 curr_axis = [xlim, ylim, zlim]; | |
4945 | 148 endif |
3667 | 149 |
5443 | 150 elseif (ischar (ax)) |
4340 | 151 len = length (ax); |
3667 | 152 |
153 ## 'matrix mode' to reverse the y-axis | |
8190
73d6b71788c0
use case-insensitive comparison for graphics properties; misc style fixes
John W. Eaton <jwe@octave.org>
parents:
7994
diff
changeset
|
154 if (strcmpi (ax, "ij")) |
6257 | 155 set (ca, "ydir", "reverse"); |
8190
73d6b71788c0
use case-insensitive comparison for graphics properties; misc style fixes
John W. Eaton <jwe@octave.org>
parents:
7994
diff
changeset
|
156 elseif (strcmpi (ax, "xy")) |
6257 | 157 set (ca, "ydir", "normal"); |
3667 | 158 |
159 ## aspect ratio | |
8190
73d6b71788c0
use case-insensitive comparison for graphics properties; misc style fixes
John W. Eaton <jwe@octave.org>
parents:
7994
diff
changeset
|
160 elseif (strcmpi (ax, "image")) |
6257 | 161 set (ca, "dataaspectratio", [1, 1, 1]); |
7376 | 162 __do_tight_option__ (ca); |
8190
73d6b71788c0
use case-insensitive comparison for graphics properties; misc style fixes
John W. Eaton <jwe@octave.org>
parents:
7994
diff
changeset
|
163 elseif (strcmpi (ax, "equal") || strcmpi (ax, "square")) |
6257 | 164 set (ca, "dataaspectratio", [1, 1, 1]); |
8190
73d6b71788c0
use case-insensitive comparison for graphics properties; misc style fixes
John W. Eaton <jwe@octave.org>
parents:
7994
diff
changeset
|
165 elseif (strcmpi (ax, "normal")) |
6257 | 166 set (ca, "dataaspectratiomode", "auto"); |
3667 | 167 |
168 ## axis limits | |
8190
73d6b71788c0
use case-insensitive comparison for graphics properties; misc style fixes
John W. Eaton <jwe@octave.org>
parents:
7994
diff
changeset
|
169 elseif (len >= 4 && strcmpi (ax(1:4), "auto")) |
4340 | 170 if (len > 4) |
6257 | 171 if (any (ax == "x")) |
172 set (ca, "xlimmode", "auto"); | |
173 endif | |
174 if (any (ax == "y")) | |
175 set (ca, "ylimmode", "auto"); | |
176 endif | |
177 if (any (ax == "z")) | |
178 set (ca, "zlimmode", "auto"); | |
179 endif | |
3667 | 180 else |
6257 | 181 set (ca, "xlimmode", "auto", "ylimmode", "auto", "zlimmode", "auto"); |
3667 | 182 endif |
8190
73d6b71788c0
use case-insensitive comparison for graphics properties; misc style fixes
John W. Eaton <jwe@octave.org>
parents:
7994
diff
changeset
|
183 elseif (strcmpi (ax, "manual")) |
3667 | 184 ## fixes the axis limits, like axis(axis) should; |
6257 | 185 set (ca, "xlimmode", "manual", "ylimmode", "manual", "zlimmode", "manual"); |
8190
73d6b71788c0
use case-insensitive comparison for graphics properties; misc style fixes
John W. Eaton <jwe@octave.org>
parents:
7994
diff
changeset
|
186 elseif (strcmpi (ax, "tight")) |
7376 | 187 ## sets the axis limits to the min and max of all data. |
188 __do_tight_option__ (ca); | |
3667 | 189 |
190 ## tic marks | |
8190
73d6b71788c0
use case-insensitive comparison for graphics properties; misc style fixes
John W. Eaton <jwe@octave.org>
parents:
7994
diff
changeset
|
191 elseif (strcmpi (ax, "on") || strcmpi (ax, "tic")) |
6257 | 192 set (ca, "xtickmode", "auto", "ytickmode", "auto", "ztickmode", "auto"); |
193 set (ca, "xticklabelmode", "auto", "yticklabelmode", "auto", | |
194 "zticklabelmode", "auto"); | |
6765 | 195 set (ca, "visible", "on"); |
8190
73d6b71788c0
use case-insensitive comparison for graphics properties; misc style fixes
John W. Eaton <jwe@octave.org>
parents:
7994
diff
changeset
|
196 elseif (strcmpi (ax, "off")) |
6257 | 197 set (ca, "xtick", [], "ytick", [], "ztick", []); |
6765 | 198 set (ca, "visible", "off"); |
8190
73d6b71788c0
use case-insensitive comparison for graphics properties; misc style fixes
John W. Eaton <jwe@octave.org>
parents:
7994
diff
changeset
|
199 elseif (len > 3 && strcmpi (ax(1:3), "tic")) |
4340 | 200 if (any (ax == "x")) |
6257 | 201 set (ca, "xtickmode", "auto"); |
3667 | 202 else |
6257 | 203 set (ca, "xtick", []); |
3667 | 204 endif |
4340 | 205 if (any (ax == "y")) |
6257 | 206 set (ca, "ytickmode", "auto"); |
3667 | 207 else |
6257 | 208 set (ca, "ytick", []); |
3667 | 209 endif |
4340 | 210 if (any (ax == "z")) |
6257 | 211 set (ca, "ztickmode", "auto"); |
3667 | 212 else |
6257 | 213 set (ca, "ztick", []); |
3667 | 214 endif |
8190
73d6b71788c0
use case-insensitive comparison for graphics properties; misc style fixes
John W. Eaton <jwe@octave.org>
parents:
7994
diff
changeset
|
215 elseif (strcmpi (ax, "label")) |
6257 | 216 set (ca, "xticklabelmode", "auto", "yticklabelmode", "auto", |
217 "zticklabelmode", "auto"); | |
8190
73d6b71788c0
use case-insensitive comparison for graphics properties; misc style fixes
John W. Eaton <jwe@octave.org>
parents:
7994
diff
changeset
|
218 elseif (strcmpi (ax, "nolabel")) |
6257 | 219 set (ca, "xticklabel", "", "yticklabel", "", "zticklabel", ""); |
8190
73d6b71788c0
use case-insensitive comparison for graphics properties; misc style fixes
John W. Eaton <jwe@octave.org>
parents:
7994
diff
changeset
|
220 elseif (len > 5 && strcmpi (ax(1:5), "label")) |
4340 | 221 if (any (ax == "x")) |
6257 | 222 set (ca, "xticklabelmode", "auto"); |
3667 | 223 else |
6257 | 224 set (ca, "xticklabel", ""); |
3667 | 225 endif |
4340 | 226 if (any (ax == "y")) |
6257 | 227 set (ca, "yticklabelmode", "auto"); |
3667 | 228 else |
6257 | 229 set (ca, "yticklabel", ""); |
3667 | 230 endif |
4340 | 231 if (any (ax == "z")) |
6257 | 232 set (ca, "zticklabelmode", "auto"); |
3667 | 233 else |
6257 | 234 set (ca, "zticklabel", ""); |
3667 | 235 endif |
236 | |
237 else | |
4259 | 238 warning ("unknown axis option '%s'", ax); |
3667 | 239 endif |
240 | |
4030 | 241 elseif (isvector (ax)) |
590 | 242 |
243 len = length (ax); | |
244 | |
245 if (len != 2 && len != 4 && len != 6) | |
246 error ("axis: expecting vector with 2, 4, or 6 elements"); | |
247 endif | |
248 | |
5627 | 249 for i = 1:2:len |
250 if (ax(i) == ax(i+1)) | |
251 error ("axis: limits(%d) cannot equal limits(%d)", i, i+1); | |
252 endif | |
253 endfor | |
254 | |
590 | 255 if (len > 1) |
6257 | 256 set (ca, "xlim", [ax(1), ax(2)]); |
590 | 257 endif |
258 | |
259 if (len > 3) | |
6257 | 260 set (ca, "ylim", [ax(3), ax(4)]); |
590 | 261 endif |
262 | |
263 if (len > 5) | |
6257 | 264 set (ca, "zlim", [ax(5), ax(6)]); |
590 | 265 endif |
266 | |
267 else | |
595 | 268 error ("axis: expecting no args, or a vector with 2, 4, or 6 elements"); |
590 | 269 endif |
270 | |
7376 | 271 if (! isempty (varargin)) |
7189 | 272 __axis__ (ca, varargin{:}); |
3667 | 273 endif |
6447 | 274 |
590 | 275 endfunction |
3667 | 276 |
7376 | 277 function lims = __get_tight_lims__ (ca, ax) |
278 | |
279 ## Get the limits for axis ("tight"). | |
280 ## AX should be one of "x", "y", or "z". | |
7994
8ccd9b0bf6bc
plot/axis.m (__get_tight_lims__): don't fail if data is not a vector
John W. Eaton <jwe@octave.org>
parents:
7376
diff
changeset
|
281 kids = findobj (ca, "-property", strcat (ax, "data")); |
7376 | 282 if (isempty (kids)) |
283 ## Return the current limits. | |
7994
8ccd9b0bf6bc
plot/axis.m (__get_tight_lims__): don't fail if data is not a vector
John W. Eaton <jwe@octave.org>
parents:
7376
diff
changeset
|
284 lims = get (ca, strcat (ax, "lim")); |
7376 | 285 else |
7994
8ccd9b0bf6bc
plot/axis.m (__get_tight_lims__): don't fail if data is not a vector
John W. Eaton <jwe@octave.org>
parents:
7376
diff
changeset
|
286 data = get (kids, strcat (ax, "data")); |
7376 | 287 if (iscell (data)) |
7994
8ccd9b0bf6bc
plot/axis.m (__get_tight_lims__): don't fail if data is not a vector
John W. Eaton <jwe@octave.org>
parents:
7376
diff
changeset
|
288 lims(1) = min (cellfun (@min, data)(:)); |
8ccd9b0bf6bc
plot/axis.m (__get_tight_lims__): don't fail if data is not a vector
John W. Eaton <jwe@octave.org>
parents:
7376
diff
changeset
|
289 lims(2) = min (cellfun (@max, data)(:)); |
7376 | 290 else |
7994
8ccd9b0bf6bc
plot/axis.m (__get_tight_lims__): don't fail if data is not a vector
John W. Eaton <jwe@octave.org>
parents:
7376
diff
changeset
|
291 lims = [min(data(:)), max(data(:))]; |
8610
85c9906abfd1
use endif and endfor instead of end
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
292 endif |
85c9906abfd1
use endif and endfor instead of end
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
293 endif |
7376 | 294 |
7994
8ccd9b0bf6bc
plot/axis.m (__get_tight_lims__): don't fail if data is not a vector
John W. Eaton <jwe@octave.org>
parents:
7376
diff
changeset
|
295 |
7376 | 296 endfunction |
297 | |
298 function __do_tight_option__ (ca) | |
299 | |
300 set (ca, | |
301 "xlim", __get_tight_lims__ (ca, "x"), | |
302 "ylim", __get_tight_lims__ (ca, "y"), | |
303 "zlim", __get_tight_lims__ (ca, "z")); | |
304 | |
305 endfunction | |
306 | |
307 | |
3667 | 308 %!demo |
309 %! t=0:0.01:2*pi; x=sin(t); | |
310 %! | |
311 %! subplot(221); title("normal plot"); | |
6746 | 312 %! plot(t, x); |
3667 | 313 %! |
314 %! subplot(222); title("square plot"); | |
6746 | 315 %! axis("square"); plot(t, x); |
3667 | 316 %! |
317 %! subplot(223); title("equal plot"); | |
6746 | 318 %! axis("equal"); plot(t, x); |
3667 | 319 %! |
320 %! subplot(224); title("normal plot again"); | |
6746 | 321 %! axis("normal"); plot(t, x); |
3667 | 322 |
323 %!demo | |
324 %! t=0:0.01:2*pi; x=sin(t); | |
325 %! | |
326 %! subplot(121); title("ij plot"); | |
6746 | 327 %! axis("ij"); plot(t, x); |
3667 | 328 %! |
329 %! subplot(122); title("xy plot"); | |
6746 | 330 %! axis("xy"); plot(t, x); |
3667 | 331 |
332 %!demo | |
333 %! t=0:0.01:2*pi; x=sin(t); | |
334 %! | |
335 %! subplot(331); title("x tics & labels"); | |
6746 | 336 %! axis("ticx"); plot(t, x); |
3667 | 337 %! |
338 %! subplot(332); title("y tics & labels"); | |
6746 | 339 %! axis("ticy"); plot(t, x); |
3667 | 340 %! |
341 %! subplot(334); title("x & y tics, x labels"); | |
6746 | 342 %! axis("labelx","tic"); plot(t, x); |
3667 | 343 %! |
344 %! subplot(335); title("x & y tics, y labels"); | |
6746 | 345 %! axis("labely","tic"); plot(t, x); |
3667 | 346 %! |
347 %! subplot(337); title("x tics, no labels"); | |
6746 | 348 %! axis("nolabel","ticx"); plot(t, x); |
3667 | 349 %! |
350 %! subplot(338); title("y tics, no labels"); | |
6746 | 351 %! axis("nolabel","ticy"); plot(t, x); |
3667 | 352 %! |
353 %! subplot(333); title("no tics or labels"); | |
6746 | 354 %! axis("off"); plot(t, x); |
3667 | 355 %! |
356 %! subplot(336); title("all tics but no labels"); | |
6746 | 357 %! axis("nolabel","tic"); plot(t, x); |
3667 | 358 %! |
359 %! subplot(339); title("all tics & labels"); | |
6746 | 360 %! axis("on"); plot(t, x); |
3667 | 361 |
362 %!demo | |
363 %! t=0:0.01:2*pi; x=sin(t); | |
364 %! | |
365 %! subplot(321); title("axes at [0 3 0 1]") | |
6746 | 366 %! axis([0,3,0,1]); plot(t, x); |
3667 | 367 %! |
368 %! subplot(322); title("auto"); | |
6746 | 369 %! axis("auto"); plot(t, x); |
3667 | 370 %! |
371 %! subplot(323); title("manual"); | |
372 %! plot(t, x, ";sine [0:2pi];"); hold on; | |
373 %! axis("manual"); | |
374 %! plot(-3:3,-3:3, ";line (-3,-3)->(3,3);"); hold off; | |
375 %! | |
376 %! subplot(324); title("axes at [0 3 0 1], then autox"); | |
377 %! axis([0,3,0,1]); axis("autox"); | |
378 %! plot(t, x, ";sine [0:2pi];"); | |
379 %! | |
380 %! subplot(325); title("axes at [3 6 0 1], then autoy"); | |
381 %! axis([3,6,0,1]); axis("autoy"); | |
382 %! plot(t, x, ";sine [0:2p];"); | |
383 %! | |
384 %! subplot(326); title("tight"); | |
6746 | 385 %! axis("tight"); plot(t, x); |
3667 | 386 %! % The last plot should not have any whitespace outside the data |
387 %! % limits, but "tight" isn't implemented yet. |