changelog shortlog tags changeset files revisions annotate raw

scripts/plot/axis.m

changeset 10289: 4b124317dc38
parent:7b74a7fd4761
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (31 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 1994, 1995, 1996, 1997, 1999, 2000, 2002, 2003, 2004,
2## 2005, 2006, 2007, 2008, 2009 John W. Eaton
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
8## the Free Software Foundation; either version 3 of the License, or (at
9## your option) any later version.
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
17## along with Octave; see the file COPYING. If not, see
18## <http://www.gnu.org/licenses/>.
19
20## -*- texinfo -*-
21## @deftypefn {Function File} {} axis (@var{limits})
22## Set axis limits for plots.
23##
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.
28##
29## Without any arguments, @code{axis} turns autoscaling on.
30##
31## With one output argument, @code{x = axis} returns the current axes
32##
33## The vector argument specifying limits is optional, and additional
34## string arguments may be used to specify various axis properties. For
35## example,
36##
37## @example
38## axis ([1, 2, 3, 4], "square");
39## @end example
40##
41## @noindent
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.
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.
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]"
90## Turn tic marks on for all axes, or turn them on for the
91## specified axes and off for the remainder.
92## @item "label[xyz]"
93## Turn tic labels on for all axes, or turn them on for the
94## specified axes and off for the remainder.
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
110##
111## If an axes handle is passed as the first argument, then operate on
112## this axes rather than the current axes.
113## @end deftypefn
114
115## Author: jwe
116
117function varargout = axis (varargin)
118
119 [h, varargin, nargin] = __plt_get_axis_arg__ ("axis", varargin{:});
120
121 oldh = gca ();
122 unwind_protect
123 axes (h);
124 varargout = cell (max (nargin == 0, nargout), 1);
125 if (isempty (varargout))
126 __axis__ (h, varargin{:});
127 else
128 [varargout{:}] = __axis__ (h, varargin{:});
129 endif
130 unwind_protect_cleanup
131 axes (oldh);
132 end_unwind_protect
133
134endfunction
135
136function curr_axis = __axis__ (ca, ax, varargin)
137
138 if (nargin == 1)
139 if (nargout == 0)
140 set (ca, "xlimmode", "auto", "ylimmode", "auto", "zlimmode", "auto");
141 else
142 xlim = get (ca, "xlim");
143 ylim = get (ca, "ylim");
144 view = get (ca, "view");
145 if (view(2) == 90)
146 curr_axis = [xlim, ylim];
147 else
148 zlim = get (ca, "zlim");
149 curr_axis = [xlim, ylim, zlim];
150 endif
151 endif
152
153 elseif (ischar (ax))
154 len = length (ax);
155
156 ## 'matrix mode' to reverse the y-axis
157 if (strcmpi (ax, "ij"))
158 set (ca, "ydir", "reverse");
159 elseif (strcmpi (ax, "xy"))
160 set (ca, "ydir", "normal");
161
162 ## aspect ratio
163 elseif (strcmpi (ax, "image"))
164 __axis__ (ca, "equal")
165 __do_tight_option__ (ca);
166 elseif (strcmpi (ax, "square"))
167 if (__gnuplot_has_feature__ ("screen_coordinates_for_{lrtb}margin"))
168 set (ca, "plotboxaspectratio", [1, 1, 1]);
169 else
170 x = xlim;
171 y = ylim;
172 set (ca, "plotboxaspectratio", [(y(2)-y(1)), (x(2)-x(1)), 1]);
173 endif
174 elseif (strcmp (ax, "equal"))
175 if (__gnuplot_has_feature__ ("screen_coordinates_for_{lrtb}margin"))
176 x = xlim;
177 y = ylim;
178 set (ca, "plotboxaspectratio", [(x(2)-x(1)), (y(2)-y(1)), 1]);
179 else
180 set (ca, "plotboxaspectratio", [1, 1, 1]);
181 endif
182 elseif (strcmpi (ax, "normal"))
183 set (ca, "plotboxaspectratiomode", "auto");
184
185 ## axis limits
186 elseif (len >= 4 && strcmpi (ax(1:4), "auto"))
187 if (len > 4)
188 if (any (ax == "x"))
189 set (ca, "xlimmode", "auto");
190 endif
191 if (any (ax == "y"))
192 set (ca, "ylimmode", "auto");
193 endif
194 if (any (ax == "z"))
195 set (ca, "zlimmode", "auto");
196 endif
197 else
198 set (ca, "xlimmode", "auto", "ylimmode", "auto", "zlimmode", "auto");
199 endif
200 elseif (strcmpi (ax, "manual"))
201 ## fixes the axis limits, like axis(axis) should;
202 set (ca, "xlimmode", "manual", "ylimmode", "manual", "zlimmode", "manual");
203 elseif (strcmpi (ax, "tight"))
204 ## sets the axis limits to the min and max of all data.
205 __do_tight_option__ (ca);
206 ## tic marks
207 elseif (strcmpi (ax, "on") || strcmpi (ax, "tic"))
208 set (ca, "xtickmode", "auto", "ytickmode", "auto", "ztickmode", "auto");
209 if (strcmpi (ax, "on"))
210 set (ca, "xticklabelmode", "auto", "yticklabelmode", "auto",
211 "zticklabelmode", "auto");
212 endif
213 set (ca, "visible", "on");
214 elseif (strcmpi (ax, "off"))
215 set (ca, "xtick", [], "ytick", [], "ztick", []);
216 set (ca, "visible", "off");
217 elseif (len > 3 && strcmpi (ax(1:3), "tic"))
218 if (any (ax == "x"))
219 set (ca, "xtickmode", "auto");
220 else
221 set (ca, "xtick", []);
222 endif
223 if (any (ax == "y"))
224 set (ca, "ytickmode", "auto");
225 else
226 set (ca, "ytick", []);
227 endif
228 if (any (ax == "z"))
229 set (ca, "ztickmode", "auto");
230 else
231 set (ca, "ztick", []);
232 endif
233 elseif (strcmpi (ax, "label"))
234 set (ca, "xticklabelmode", "auto", "yticklabelmode", "auto",
235 "zticklabelmode", "auto");
236 elseif (strcmpi (ax, "nolabel"))
237 set (ca, "xticklabel", "", "yticklabel", "", "zticklabel", "");
238 elseif (len > 5 && strcmpi (ax(1:5), "label"))
239 if (any (ax == "x"))
240 set (ca, "xticklabelmode", "auto");
241 else
242 set (ca, "xticklabel", "");
243 endif
244 if (any (ax == "y"))
245 set (ca, "yticklabelmode", "auto");
246 else
247 set (ca, "yticklabel", "");
248 endif
249 if (any (ax == "z"))
250 set (ca, "zticklabelmode", "auto");
251 else
252 set (ca, "zticklabel", "");
253 endif
254
255 else
256 warning ("unknown axis option '%s'", ax);
257 endif
258
259 elseif (isvector (ax))
260
261 len = length (ax);
262
263 if (len != 2 && len != 4 && len != 6)
264 error ("axis: expecting vector with 2, 4, or 6 elements");
265 endif
266
267 for i = 1:2:len
268 if (ax(i) == ax(i+1))
269 error ("axis: limits(%d) cannot equal limits(%d)", i, i+1);
270 endif
271 endfor
272
273 if (len > 1)
274 set (ca, "xlim", [ax(1), ax(2)]);
275 endif
276
277 if (len > 3)
278 set (ca, "ylim", [ax(3), ax(4)]);
279 endif
280
281 if (len > 5)
282 set (ca, "zlim", [ax(5), ax(6)]);
283 endif
284
285 else
286 error ("axis: expecting no args, or a vector with 2, 4, or 6 elements");
287 endif
288
289 if (! isempty (varargin))
290 __axis__ (ca, varargin{:});
291 endif
292
293endfunction
294
295function lims = __get_tight_lims__ (ca, ax)
296
297 ## Get the limits for axis ("tight").
298 ## AX should be one of "x", "y", or "z".
299 kids = findobj (ca, "-property", strcat (ax, "data"));
300 if (isempty (kids))
301 ## Return the current limits.
302 lims = get (ca, strcat (ax, "lim"));
303 else
304 data = get (kids, strcat (ax, "data"));
305 if (iscell (data))
306 data = data (find (! cellfun (@isempty, data)));
307 if (! isempty (data))
308 lims_min = min (cellfun (@min, cellfun (@min, data, 'UniformOutput', false)(:)));
309 lims_max = max (cellfun (@max, cellfun (@max, data, 'UniformOutput', false)(:)));
310 lims = [lims_min, lims_max];
311 else
312 lims = [0, 1];
313 endif
314 else
315 lims = [min(data(:)), max(data(:))];
316 endif
317 endif
318
319
320endfunction
321
322function __do_tight_option__ (ca)
323
324 set (ca,
325 "xlim", __get_tight_lims__ (ca, "x"),
326 "ylim", __get_tight_lims__ (ca, "y"),
327 "zlim", __get_tight_lims__ (ca, "z"));
328
329endfunction
330
331%!demo
332%! t=0:0.01:2*pi; x=sin(t);
333%!
334%! subplot(221);
335%! plot(t, x);
336%! title("normal plot");
337%!
338%! subplot(222);
339%! plot(t, x);
340%! title("square plot");
341%! axis("square");
342%!
343%! subplot(223);
344%! plot(t, x);
345%! title("equal plot");
346%! axis("equal");
347%!
348%! subplot(224);
349%! plot(t, x);
350%! title("normal plot again");
351%! axis("normal");
352
353%!demo
354%! t=0:0.01:2*pi; x=sin(t);
355%!
356%! subplot(121);
357%! plot(t, x);
358%! title("ij plot");
359%! axis("ij");
360%!
361%! subplot(122);
362%! plot(t, x);
363%! title("xy plot");
364%! axis("xy");
365
366%!demo
367%! t=0:0.01:2*pi; x=sin(t);
368%!
369%! subplot(331);
370%! plot(t, x);
371%! title("x tics and labels");
372%! axis("ticx");
373%!
374%! subplot(332);
375%! plot(t, x);
376%! title("y tics and labels");
377%! axis("ticy");
378%!
379%! subplot(333);
380%! plot(t, x);
381%! title("axis off");
382%! axis("off");
383%!
384%! subplot(334);
385%! plot(t, x);
386%! title("x and y tics, x labels");
387%! axis("labelx","tic");
388%!
389%! subplot(335);
390%! plot(t, x);
391%! title("x and y tics, y labels");
392%! axis("labely","tic");
393%!
394%! subplot(336);
395%! plot(t, x);
396%! title("all tics but no labels");
397%! axis("nolabel","tic");
398%!
399%! subplot(337);
400%! plot(t, x);
401%! title("x tics, no labels");
402%! axis("nolabel","ticx");
403%!
404%! subplot(338);
405%! plot(t, x);
406%! title("y tics, no labels");
407%! axis("nolabel","ticy");
408%!
409%! subplot(339);
410%! plot(t, x);
411%! title("all tics and labels");
412%! axis("on");
413
414%!demo
415%! t=0:0.01:2*pi; x=sin(t);
416%!
417%! subplot(321);
418%! plot(t, x);
419%! title("axes at [0 3 0 1]")
420%! axis([0,3,0,1]);
421%!
422%! subplot(322);
423%! plot(t, x);
424%! title("auto");
425%! axis("auto");
426%!
427%! subplot(323);
428%! plot(t, x, ";sine [0:2pi];"); hold on;
429%! plot(-3:3,-3:3, ";line (-3,-3)->(3,3);"); hold off;
430%! title("manual");
431%! axis("manual");
432%!
433%! subplot(324);
434%! plot(t, x, ";sine [0:2pi];");
435%! title("axes at [0 3 0 1], then autox");
436%! axis([0,3,0,1]); axis("autox");
437%!
438%! subplot(325);
439%! plot(t, x, ";sine [0:2p];");
440%! axis([3,6,0,1]); axis("autoy");
441%! title("axes at [3 6 0 1], then autoy");
442%!
443%! subplot(326);
444%! plot(t, sin(t), t, -2*sin(t/2))
445%! axis("tight");
446%! title("tight");
447
448%!demo
449%! clf
450%! axis image
451%! x=0:0.1:10;
452%! plot(x,sin(x))
453%! axis image
454%! title("image")
455
456%!demo
457%! clf
458%! [x,y,z] = peaks(50);
459%! x1 = max(x(:));
460%! pcolor(x-x1,y-x1/2,z)
461%! hold on
462%! [x,y,z] = sombrero;
463%! s = x1/max(x(:));
464%! pcolor(s*x+x1,s*y+x1/2,5*z)
465%! axis tight
466