2847
|
1 ## Copyright (C) 1996, 1997 John W. Eaton |
2313
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, USA. |
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 ## |
3368
|
29 ## If your plot is already drawn, then you need to use @code{replot} before |
|
30 ## the new axis limits will take effect. You can get this to happen |
|
31 ## automatically by setting the built-in variable @code{automatic_replot} |
|
32 ## to a nonzero value. |
3667
|
33 ## |
|
34 ## Without any arguments, @code{axis} turns autoscaling on. |
|
35 ## |
3668
|
36 ## The vector argument specifying limits is optional, and additional |
|
37 ## string arguments may be used to specify various axis properties. For |
3667
|
38 ## example, |
|
39 ## |
|
40 ## @example |
|
41 ## axis ([1, 2, 3, 4], "square"); |
|
42 ## @end example |
|
43 ## |
|
44 ## @noindent |
3668
|
45 ## forces a square aspect ratio, and |
|
46 ## |
|
47 ## @example |
|
48 ## axis ("labely", "tic"); |
|
49 ## @end example |
|
50 ## |
|
51 ## @noindent |
|
52 ## turns tic marks on for all axes and tic mark labels on for the y-axis |
|
53 ## only. |
3667
|
54 ## |
|
55 ## @noindent |
|
56 ## The following options control the aspect ratio of the axes. |
|
57 ## |
|
58 ## @table @code |
|
59 ## @item "square" |
|
60 ## Force a square aspect ratio. |
|
61 ## @item "equal" |
|
62 ## Force x distance to equal y-distance. |
|
63 ## @item "normal" |
|
64 ## Restore the balance. |
|
65 ## @end table |
|
66 ## |
|
67 ## @noindent |
|
68 ## The following options control the way axis limits are interpreted. |
|
69 ## |
|
70 ## @table @code |
|
71 ## @item "auto" |
|
72 ## Set the specified axes to have nice limits around the data |
|
73 ## or all if no axes are specified. |
|
74 ## @item "manual" |
|
75 ## Fix the current axes limits. |
|
76 ## @item "tight" |
|
77 ## Fix axes to the limits of the data (not implemented). |
|
78 ## @end table |
|
79 ## |
|
80 ## @noindent |
|
81 ## The option @code{"image"} is equivalent to @code{"tight"} and |
|
82 ## @code{"equal"}. |
|
83 ## |
|
84 ## @noindent |
|
85 ## The following options affect the appearance of tic marks. |
|
86 ## |
|
87 ## @table @code |
|
88 ## @item "on" |
|
89 ## Turn tic marks and labels on for all axes. |
|
90 ## @item "off" |
|
91 ## Turn tic marks off for all axes. |
|
92 ## @item "tic[xyz]" |
3668
|
93 ## Turn tic marks on for all axes, or turn them on for the |
|
94 ## specified axes and off for the remainder. |
3667
|
95 ## @item "label[xyz]" |
3668
|
96 ## Turn tic labels on for all axes, or turn them on for the |
|
97 ## specified axes and off for the remainder. |
3667
|
98 ## @item "nolabel" |
|
99 ## Turn tic labels off for all axes. |
|
100 ## @end table |
|
101 ## Note, if there are no tic marks for an axis, there can be no labels. |
|
102 ## |
|
103 ## @noindent |
|
104 ## The following options affect the direction of increasing values on |
|
105 ## the axes. |
|
106 ## |
|
107 ## @table @code |
|
108 ## @item "ij" |
|
109 ## Reverse y-axis, so lower values are nearer the top. |
|
110 ## @item "xy" |
|
111 ## Restore y-axis, so higher values are nearer the top. |
|
112 ## @end table |
3368
|
113 ## @end deftypefn |
590
|
114 |
2314
|
115 ## Author: jwe |
|
116 |
3979
|
117 function curr_axis = axis (ax, varargin) |
590
|
118 |
2303
|
119 ## This may not be correct if someone has used the gnuplot interface |
|
120 ## directly... |
1610
|
121 |
3106
|
122 global __current_axis__ = [-10, 10, -10, 10]; |
1610
|
123 |
3667
|
124 ## To return curr_axis properly, octave needs to take control of scaling. |
|
125 ## It isn't hard to compute good axis limits: |
|
126 ## scale = 10 ^ floor (log10 (max - min) - 1); |
|
127 ## r = scale * [floor (min / scale), ceil (max / scale)]; |
|
128 ## However, with axis("manual") there is little need to know the current |
|
129 ## limits. |
590
|
130 |
595
|
131 if (nargin == 0) |
2520
|
132 gset autoscale; |
1610
|
133 curr_axis = __current_axis__; |
3667
|
134 |
|
135 elseif (isstr (ax)) |
|
136 ax = tolower (ax); |
|
137 |
|
138 ## 'matrix mode' to reverse the y-axis |
|
139 if (strcmp (ax, "ij")) |
|
140 gset yrange [] reverse; |
|
141 elseif (strcmp (ax, "xy")) |
|
142 gset yrange [] noreverse; |
|
143 |
|
144 ## aspect ratio |
|
145 elseif (strcmp (ax, "image")) |
|
146 gset size ratio -1; |
|
147 gset autoscale; ## XXX FIXME XXX should be the same as "tight" |
|
148 elseif (strcmp (ax, "equal")) |
|
149 gset size ratio -1; |
|
150 elseif (strcmp (ax, "square")) |
|
151 gset size ratio 1; |
|
152 elseif (strcmp (ax, "normal")) |
|
153 gset size noratio; |
|
154 |
|
155 |
|
156 ## axis limits |
|
157 elseif (length (ax) >= 4 && strcmp (ax (1:4), "auto")) |
|
158 if length (ax) > 4 |
|
159 eval (["gset autoscale ", ax (5 : length (ax)), ";"]); |
|
160 else |
|
161 gset autoscale; |
|
162 endif |
|
163 elseif (strcmp (ax, "manual")) |
|
164 ## fixes the axis limits, like axis(axis) should; |
|
165 gset xrange [] writeback; |
|
166 gset yrange [] writeback; |
|
167 gset zrange [] writeback; |
|
168 ## XXX FIXME XXX if writeback were set in plot, no need to replot here. |
|
169 replot; |
|
170 gset noautoscale x; |
|
171 gset noautoscale y; |
|
172 gset noautoscale z; |
|
173 elseif (strcmp (ax, "tight")) |
|
174 ## XXX FIXME XXX if tight, plot must set ranges to limits of the |
|
175 ## all the data on the current plot, even if from a previous call. |
|
176 ## Instead, just let gnuplot do as it likes. |
|
177 gset autoscale; |
|
178 |
|
179 |
|
180 ## tic marks |
|
181 elseif (strcmp (ax, "on")) |
|
182 gset xtics; |
|
183 gset ytics; |
|
184 gset ztics; |
|
185 gset format; |
|
186 elseif (strcmp (ax, "off")) |
|
187 gset noxtics; |
|
188 gset noytics; |
|
189 gset noztics; |
|
190 elseif (strcmp (ax, "tic")) |
|
191 gset xtics; |
|
192 gset ytics; |
|
193 gset ztics; |
|
194 elseif (length (ax) > 3 && strcmp (ax (1:3), "tic")) |
|
195 if any (ax == "x") |
|
196 gset xtics; |
|
197 else |
|
198 gset noxtics; |
|
199 endif |
|
200 if any (ax == "y") |
|
201 gset ytics; |
|
202 else |
|
203 gset noytics; |
|
204 endif |
|
205 if any (ax == "z") |
|
206 gset ztics; |
|
207 else |
|
208 gset noztics; |
|
209 endif |
|
210 elseif (strcmp (ax, "label")) |
|
211 gset format; |
|
212 elseif (strcmp (ax, "nolabel")) |
|
213 gset format "\\0"; |
|
214 elseif (length (ax) > 5 && strcmp (ax (1:5), "label")) |
|
215 if any (ax == "x") |
|
216 gset format x; |
|
217 else |
|
218 gset format x "\\0"; |
|
219 endif |
|
220 if any (ax == "y") |
|
221 gset format y; |
|
222 else |
|
223 gset format y "\\0"; |
|
224 endif |
|
225 if any (ax == "z") |
|
226 gset format z; |
|
227 else |
|
228 gset format z "\\0"; |
|
229 endif |
|
230 |
|
231 else |
|
232 warning (["unknown axis option '", ax, "'"]); |
|
233 endif |
|
234 |
595
|
235 elseif (is_vector (ax)) |
590
|
236 |
|
237 len = length (ax); |
|
238 |
|
239 if (len != 2 && len != 4 && len != 6) |
|
240 error ("axis: expecting vector with 2, 4, or 6 elements"); |
|
241 endif |
|
242 |
1610
|
243 __current_axis__ = reshape (ax, 1, len); |
|
244 |
590
|
245 if (len > 1) |
2520
|
246 eval (sprintf ("gset xrange [%g:%g];", ax (1), ax (2))); |
590
|
247 endif |
|
248 |
|
249 if (len > 3) |
2520
|
250 eval (sprintf ("gset yrange [%g:%g];", ax (3), ax (4))); |
590
|
251 endif |
|
252 |
|
253 if (len > 5) |
2520
|
254 eval (sprintf ("gset zrange [%g:%g];", ax (5), ax (6))); |
590
|
255 endif |
|
256 |
|
257 else |
595
|
258 error ("axis: expecting no args, or a vector with 2, 4, or 6 elements"); |
590
|
259 endif |
|
260 |
3667
|
261 if (nargin > 1) |
3979
|
262 axis (varargin{:}); |
3667
|
263 endif |
590
|
264 endfunction |
3667
|
265 |
|
266 %!demo |
|
267 %! t=0:0.01:2*pi; x=sin(t); |
|
268 %! |
|
269 %! subplot(221); title("normal plot"); |
|
270 %! plot(t, x, ";;"); |
|
271 %! |
|
272 %! subplot(222); title("square plot"); |
|
273 %! axis("square"); plot(t, x, ";;"); |
|
274 %! |
|
275 %! subplot(223); title("equal plot"); |
|
276 %! axis("equal"); plot(t, x, ";;"); |
|
277 %! |
|
278 %! subplot(224); title("normal plot again"); |
|
279 %! axis("normal"); plot(t, x, ";;"); |
|
280 |
|
281 %!demo |
|
282 %! t=0:0.01:2*pi; x=sin(t); |
|
283 %! |
|
284 %! subplot(121); title("ij plot"); |
|
285 %! axis("ij"); plot(t, x, ";;"); |
|
286 %! |
|
287 %! subplot(122); title("xy plot"); |
|
288 %! axis("xy"); plot(t, x, ";;"); |
|
289 |
|
290 %!demo |
|
291 %! t=0:0.01:2*pi; x=sin(t); |
|
292 %! |
|
293 %! subplot(331); title("x tics & labels"); |
|
294 %! axis("ticx"); plot(t, x, ";;"); |
|
295 %! |
|
296 %! subplot(332); title("y tics & labels"); |
|
297 %! axis("ticy"); plot(t, x, ";;"); |
|
298 %! |
|
299 %! subplot(334); title("x & y tics, x labels"); |
|
300 %! axis("labelx","tic"); plot(t, x, ";;"); |
|
301 %! |
|
302 %! subplot(335); title("x & y tics, y labels"); |
|
303 %! axis("labely","tic"); plot(t, x, ";;"); |
|
304 %! |
|
305 %! subplot(337); title("x tics, no labels"); |
|
306 %! axis("nolabel","ticx"); plot(t, x, ";;"); |
|
307 %! |
|
308 %! subplot(338); title("y tics, no labels"); |
|
309 %! axis("nolabel","ticy"); plot(t, x, ";;"); |
|
310 %! |
|
311 %! subplot(333); title("no tics or labels"); |
|
312 %! axis("off"); plot(t, x, ";;"); |
|
313 %! |
|
314 %! subplot(336); title("all tics but no labels"); |
|
315 %! axis("nolabel","tic"); plot(t, x, ";;"); |
|
316 %! |
|
317 %! subplot(339); title("all tics & labels"); |
|
318 %! axis("on"); plot(t, x, ";;"); |
|
319 |
|
320 %!demo |
|
321 %! t=0:0.01:2*pi; x=sin(t); |
|
322 %! |
|
323 %! subplot(321); title("axes at [0 3 0 1]") |
|
324 %! axis([0,3,0,1]); plot(t, x, ";;"); |
|
325 %! |
|
326 %! subplot(322); title("auto"); |
|
327 %! axis("auto"); plot(t, x, ";;"); |
|
328 %! |
|
329 %! subplot(323); title("manual"); |
|
330 %! plot(t, x, ";sine [0:2pi];"); hold on; |
|
331 %! axis("manual"); |
|
332 %! plot(-3:3,-3:3, ";line (-3,-3)->(3,3);"); hold off; |
|
333 %! |
|
334 %! subplot(324); title("axes at [0 3 0 1], then autox"); |
|
335 %! axis([0,3,0,1]); axis("autox"); |
|
336 %! plot(t, x, ";sine [0:2pi];"); |
|
337 %! |
|
338 %! subplot(325); title("axes at [3 6 0 1], then autoy"); |
|
339 %! axis([3,6,0,1]); axis("autoy"); |
|
340 %! plot(t, x, ";sine [0:2p];"); |
|
341 %! |
|
342 %! subplot(326); title("tight"); |
|
343 %! axis("tight"); plot(t, x, ";;"); |
|
344 %! % The last plot should not have any whitespace outside the data |
|
345 %! % limits, but "tight" isn't implemented yet. |