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