1## Copyright (C) 1994, 1995, 1996, 1997, 1999, 2000, 2002, 2003, 2004,
2## 2005, 2006, 2007, 2008, 2009 John W. Eaton
4## This file is part of Octave.
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.
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.
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/>.
21## @deftypefn {Function File} {} axis (@var{limits})
22## Set axis limits for plots.
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.
29## Without any arguments, @code{axis} turns autoscaling on.
31## With one output argument, @code{x = axis} returns the current axes
33## The vector argument specifying limits is optional, and additional
34## string arguments may be used to specify various axis properties. For
38## axis ([1, 2, 3, 4], "square");
42## forces a square aspect ratio, and
45## axis ("labely", "tic");
49## turns tic marks on for all axes and tic mark labels on for the y-axis
53## The following options control the aspect ratio of the axes.
57## Force a square aspect ratio.
59## Force x distance to equal y-distance.
61## Restore the balance.
65## The following options control the way axis limits are interpreted.
69## Set the specified axes to have nice limits around the data
70## or all if no axes are specified.
72## Fix the current axes limits.
74## Fix axes to the limits of the data.
78## The option @code{"image"} is equivalent to @code{"tight"} and
82## The following options affect the appearance of tic marks.
86## Turn tic marks and labels on for all axes.
88## Turn tic marks off for all axes.
90## Turn tic marks on for all axes, or turn them on for the
91## specified axes and off for the remainder.
93## Turn tic labels on for all axes, or turn them on for the
94## specified axes and off for the remainder.
96## Turn tic labels off for all axes.
98## Note, if there are no tic marks for an axis, there can be no labels.
101## The following options affect the direction of increasing values on
106## Reverse y-axis, so lower values are nearer the top.
108## Restore y-axis, so higher values are nearer the top.
111## If an axes handle is passed as the first argument, then operate on
112## this axes rather than the current axes.
117function varargout = axis (varargin)
119 [h, varargin, nargin] = __plt_get_axis_arg__ ("axis", varargin{:});
124 varargout = cell (max (nargin == 0, nargout), 1);
125 if (isempty (varargout))
126 __axis__ (h, varargin{:});
128 [varargout{:}] = __axis__ (h, varargin{:});
130 unwind_protect_cleanup
136function curr_axis = __axis__ (ca, ax, varargin)
140 set (ca, "xlimmode", "auto", "ylimmode", "auto", "zlimmode", "auto");
142 xlim = get (ca, "xlim");
143 ylim = get (ca, "ylim");
144 view = get (ca, "view");
146 curr_axis = [xlim, ylim];
148 zlim = get (ca, "zlim");
149 curr_axis = [xlim, ylim, zlim];
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");
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]);
172 set (ca, "plotboxaspectratio", [(y(2)-y(1)), (x(2)-x(1)), 1]);
174 elseif (strcmp (ax, "equal"))
175 if (__gnuplot_has_feature__ ("screen_coordinates_for_{lrtb}margin"))
178 set (ca, "plotboxaspectratio", [(x(2)-x(1)), (y(2)-y(1)), 1]);
180 set (ca, "plotboxaspectratio", [1, 1, 1]);
182 elseif (strcmpi (ax, "normal"))
183 set (ca, "plotboxaspectratiomode", "auto");
186 elseif (len >= 4 && strcmpi (ax(1:4), "auto"))
189 set (ca, "xlimmode", "auto");
192 set (ca, "ylimmode", "auto");
195 set (ca, "zlimmode", "auto");
198 set (ca, "xlimmode", "auto", "ylimmode", "auto", "zlimmode", "auto");
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);
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");
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"))
219 set (ca, "xtickmode", "auto");
221 set (ca, "xtick", []);
224 set (ca, "ytickmode", "auto");
226 set (ca, "ytick", []);
229 set (ca, "ztickmode", "auto");
231 set (ca, "ztick", []);
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"))
240 set (ca, "xticklabelmode", "auto");
242 set (ca, "xticklabel", "");
245 set (ca, "yticklabelmode", "auto");
247 set (ca, "yticklabel", "");
250 set (ca, "zticklabelmode", "auto");
252 set (ca, "zticklabel", "");
256 warning ("unknown axis option '%s'", ax);
259 elseif (isvector (ax))
263 if (len != 2 && len != 4 && len != 6)
264 error ("axis: expecting vector with 2, 4, or 6 elements");
268 if (ax(i) == ax(i+1))
269 error ("axis: limits(%d) cannot equal limits(%d)", i, i+1);
274 set (ca, "xlim", [ax(1), ax(2)]);
278 set (ca, "ylim", [ax(3), ax(4)]);
282 set (ca, "zlim", [ax(5), ax(6)]);
286 error ("axis: expecting no args, or a vector with 2, 4, or 6 elements");
289 if (! isempty (varargin))
290 __axis__ (ca, varargin{:});
295function lims = __get_tight_lims__ (ca, ax)
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"));
301 ## Return the current limits.
302 lims = get (ca, strcat (ax, "lim"));
304 data = get (kids, strcat (ax, "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];
315 lims = [min(data(:)), max(data(:))];
322function __do_tight_option__ (ca)
325 "xlim", __get_tight_lims__ (ca, "x"),
326 "ylim", __get_tight_lims__ (ca, "y"),
327 "zlim", __get_tight_lims__ (ca, "z"));
332%! t=0:0.01:2*pi; x=sin(t);
336%! title("normal plot");
340%! title("square plot");
345%! title("equal plot");
350%! title("normal plot again");
354%! t=0:0.01:2*pi; x=sin(t);
367%! t=0:0.01:2*pi; x=sin(t);
371%! title("x tics and labels");
376%! title("y tics and labels");
386%! title("x and y tics, x labels");
387%! axis("labelx","tic");
391%! title("x and y tics, y labels");
392%! axis("labely","tic");
396%! title("all tics but no labels");
397%! axis("nolabel","tic");
401%! title("x tics, no labels");
402%! axis("nolabel","ticx");
406%! title("y tics, no labels");
407%! axis("nolabel","ticy");
411%! title("all tics and labels");
415%! t=0:0.01:2*pi; x=sin(t);
419%! title("axes at [0 3 0 1]")
428%! plot(t, x, ";sine [0:2pi];"); hold on;
429%! plot(-3:3,-3:3, ";line (-3,-3)->(3,3);"); hold off;
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");
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");
444%! plot(t, sin(t), t, -2*sin(t/2))
458%! [x,y,z] = peaks(50);
460%! pcolor(x-x1,y-x1/2,z)
462%! [x,y,z] = sombrero;
464%! pcolor(s*x+x1,s*y+x1/2,5*z)