Mercurial > hg > octave-nkf
annotate scripts/plot/plot3.m @ 8890:ae51d068bbd5
__actual_axis_position__.m: New function to determine position of rendered axes.
author | Ben Abbott <bpabbott@mac.com> |
---|---|
date | Sat, 28 Feb 2009 22:39:33 -0500 |
parents | 6f2d95255911 |
children | eb63fbe60fab |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1996, 2006, 2007 John W. Eaton |
5837 | 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
5837 | 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 | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
5837 | 18 |
19 ## -*- texinfo -*- | |
5910 | 20 ## @deftypefn {Function File} {} plot3 (@var{args}) |
6895 | 21 ## Produce three-dimensional plots. Many different combinations of |
22 ## arguments are possible. The simplest form is | |
5837 | 23 ## |
24 ## @example | |
25 ## plot3 (@var{x}, @var{y}, @var{z}) | |
26 ## @end example | |
27 ## | |
28 ## @noindent | |
6895 | 29 ## in which the arguments are taken to be the vertices of the points to |
30 ## be plotted in three dimensions. If all arguments are vectors of the | |
31 ## same length, then a single continuous line is drawn. If all arguments | |
32 ## are matrices, then each column of the matrices is treated as a | |
7001 | 33 ## separate line. No attempt is made to transpose the arguments to make |
6895 | 34 ## the number of rows match. |
5837 | 35 ## |
6895 | 36 ## If only two arguments are given, as |
5910 | 37 ## |
38 ## @example | |
39 ## plot3 (@var{x}, @var{c}) | |
40 ## @end example | |
41 ## | |
6895 | 42 ## @noindent |
43 ## the real and imaginary parts of the second argument are used | |
44 ## as the @var{y} and @var{z} coordinates, respectively. | |
5910 | 45 ## |
46 ## If only one argument is given, as | |
47 ## | |
48 ## @example | |
49 ## plot3 (@var{c}) | |
50 ## @end example | |
51 ## | |
6895 | 52 ## @noindent |
5910 | 53 ## the real and imaginary parts of the argument are used as the @var{y} |
54 ## and @var{z} values, and they are plotted versus their index. | |
55 ## | |
6895 | 56 ## Arguments may also be given in groups of three as |
5837 | 57 ## |
58 ## @example | |
6146 | 59 ## plot3 (@var{x1}, @var{y1}, @var{z1}, @var{x2}, @var{y2}, @var{z2}, @dots{}) |
5837 | 60 ## @end example |
61 ## | |
62 ## @noindent | |
7001 | 63 ## in which each set of three arguments is treated as a separate line or |
5910 | 64 ## set of lines in three dimensions. |
65 ## | |
6895 | 66 ## To plot multiple one- or two-argument groups, separate each group |
67 ## with an empty format string, as | |
5910 | 68 ## |
69 ## @example | |
6895 | 70 ## plot3 (@var{x1}, @var{c1}, "", @var{c2}, "", @dots{}) |
5910 | 71 ## @end example |
5837 | 72 ## |
6895 | 73 ## An example of the use of @code{plot3} is |
5837 | 74 ## |
75 ## @example | |
76 ## @group | |
77 ## z = [0:0.05:5]; | |
6895 | 78 ## plot3 (cos(2*pi*z), sin(2*pi*z), z, ";helix;"); |
79 ## plot3 (z, exp(2i*pi*z), ";complex sinusoid;"); | |
5837 | 80 ## @end group |
81 ## @end example | |
8286
6f2d95255911
fix @seealso references to point to existing anchors
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
8257
diff
changeset
|
82 ## @seealso{plot, xlabel, ylabel, zlabel, title, print} |
5837 | 83 ## @end deftypefn |
84 | |
85 ## Author: Paul Kienzle | |
86 ## (modified from __plt__.m) | |
87 | |
6302 | 88 function retval = plot3 (varargin) |
5837 | 89 |
6560 | 90 newplot (); |
91 | |
6257 | 92 x_set = 0; |
93 y_set = 0; | |
94 z_set = 0; | |
6459 | 95 property_set = 0; |
96 fmt_set = 0; | |
97 properties = {}; | |
5837 | 98 |
6302 | 99 idx = 0; |
100 | |
6257 | 101 ## Gather arguments, decode format, and plot lines. |
6459 | 102 arg = 0; |
103 while (arg++ < nargin) | |
6257 | 104 new = varargin{arg}; |
6459 | 105 new_cell = varargin(arg); |
106 | |
107 if (property_set) | |
108 properties = [properties, new_cell]; | |
109 property_set = 0; | |
110 continue; | |
111 endif | |
6004 | 112 |
6257 | 113 if (ischar (new)) |
114 if (! z_set) | |
115 if (! y_set) | |
116 if (! x_set) | |
117 error ("plot3: needs x, [ y, [ z ] ]"); | |
6004 | 118 else |
6257 | 119 z = imag (x); |
120 y = real (x); | |
121 y_set = 1; | |
122 z_set = 1; | |
123 if (rows(x) > 1) | |
124 x = repmat ((1:rows(x))', 1, columns(x)); | |
125 else | |
126 x = 1:columns(x); | |
127 endif | |
6004 | 128 endif |
6257 | 129 else |
130 z = imag (y); | |
131 y = real (y); | |
132 z_set = 1; | |
6004 | 133 endif |
5837 | 134 endif |
6459 | 135 |
136 if (! fmt_set) | |
137 [options, valid] = __pltopt__ ("plot3", new, false); | |
138 if (! valid) | |
139 properties = [properties, new_cell]; | |
140 property_set = 1; | |
141 continue; | |
142 else | |
143 fmt_set = 1; | |
144 while (arg < nargin && ischar (varargin{arg+1})) | |
145 if (nargin - arg < 2) | |
146 error ("plot3: properties must appear followed by a value"); | |
147 endif | |
148 properties = [properties, varargin(arg:arg+1)]; | |
149 arg += 2; | |
150 endwhile | |
151 endif | |
152 else | |
153 properties = [properties, new_cell]; | |
154 property_set = 1; | |
155 continue; | |
156 endif | |
6004 | 157 |
158 if (isvector (x) && isvector (y)) | |
159 if (isvector (z)) | |
160 x = x(:); | |
161 y = y(:); | |
162 z = z(:); | |
163 elseif (length (x) == rows (z) && length (y) == columns (z)) | |
8078
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
164 [x, y] = meshgrid (x, y); |
6004 | 165 else |
8078
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
166 error ("plot3: [length(x), length(y)] must match size(z)"); |
6004 | 167 endif |
168 endif | |
169 | |
7292 | 170 if (! size_equal (x, y, z)) |
6004 | 171 error ("plot3: x, y, and z must have the same shape"); |
172 endif | |
173 | |
6264 | 174 key = options.key; |
175 if (! isempty (key)) | |
176 set (gca (), "key", "on"); | |
177 endif | |
8078
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
178 |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
179 for i = 1 : columns (x) |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
180 color = options.color; |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
181 if (isempty (options.color)) |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
182 color = __next_line_color__ (); |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
183 endif |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
184 |
8257 | 185 tmp(++idx) = line (x(:, i), y(:, i), z(:, i), "keylabel", key, |
186 "color", color, | |
187 "linestyle", options.linestyle, | |
188 "marker", options.marker, properties{:}); | |
8078
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
189 endfor |
6004 | 190 |
6257 | 191 x_set = 0; |
192 y_set = 0; | |
193 z_set = 0; | |
6459 | 194 fmt_set = 0; |
195 properties = {}; | |
6257 | 196 elseif (! x_set) |
197 x = new; | |
198 x_set = 1; | |
199 elseif (! y_set) | |
200 y = new; | |
201 y_set = 1; | |
202 elseif (! z_set) | |
203 z = new; | |
204 z_set = 1; | |
205 else | |
206 if (isvector (x) && isvector (y)) | |
207 if (isvector (z)) | |
208 x = x(:); | |
209 y = y(:); | |
210 z = z(:); | |
211 elseif (length (x) == rows (z) && length (y) == columns (z)) | |
8078
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
212 [x, y] = meshgrid (x, y); |
6257 | 213 else |
8078
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
214 error ("plot3: [length(x), length(y)] must match size(z)"); |
6257 | 215 endif |
216 endif | |
217 | |
7292 | 218 if (! size_equal (x, y, z)) |
6257 | 219 error ("plot3: x, y, and z must have the same shape"); |
220 endif | |
221 | |
6459 | 222 options = __default_plot_options__ (); |
223 key = options.key; | |
224 if (! isempty (key)) | |
225 set (gca (), "key", "on"); | |
226 endif | |
8078
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
227 |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
228 for i = 1 : columns (x) |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
229 color = options.color; |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
230 if (isempty (color)) |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
231 color = __next_line_color__ (); |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
232 endif |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
233 |
8257 | 234 tmp(++idx) = line (x(:, i), y(:, i), z(:, i), "keylabel", key, |
235 "color", color, | |
236 "linestyle", options.linestyle, | |
237 "marker", options.marker, properties{:}); | |
8078
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
238 endfor |
6257 | 239 |
240 x = new; | |
241 y_set = 0; | |
242 z_set = 0; | |
6459 | 243 fmt_set = 0; |
244 properties = {}; | |
5837 | 245 endif |
6257 | 246 |
6459 | 247 endwhile |
248 | |
249 if (property_set) | |
250 error ("plot3: properties must appear followed by a value"); | |
251 endif | |
6257 | 252 |
253 ## Handle last plot. | |
254 | |
255 if (x_set) | |
256 if (y_set) | |
257 if (! z_set) | |
258 z = imag (y); | |
259 y = real (y); | |
260 z_set = 1; | |
261 endif | |
262 else | |
263 z = imag (x); | |
264 y = real (x); | |
265 y_set = 1; | |
266 z_set = 1; | |
267 if (rows (x) > 1) | |
268 x = repmat ((1:rows (x))', 1, columns(x)); | |
269 else | |
270 x = 1:columns(x); | |
271 endif | |
5837 | 272 endif |
6257 | 273 |
274 if (isvector (x) && isvector (y)) | |
275 if (isvector (z)) | |
276 x = x(:); | |
277 y = y(:); | |
278 z = z(:); | |
279 elseif (length (x) == rows (z) && length (y) == columns (z)) | |
8078
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
280 [x, y] = meshgrid (x, y); |
6257 | 281 else |
8078
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
282 error ("plot3: [length(x), length(y)] must match size(z)"); |
6257 | 283 endif |
284 endif | |
285 | |
7292 | 286 if (! size_equal (x, y, z)) |
6257 | 287 error ("plot3: x, y, and z must have the same shape"); |
288 endif | |
289 | |
6459 | 290 options = __default_plot_options__ (); |
291 key = options.key; | |
292 if (! isempty (key)) | |
293 set (gca (), "key", "on"); | |
294 endif | |
8078
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
295 |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
296 for i = 1 : columns (x) |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
297 color = options.color; |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
298 if (isempty (color)) |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
299 color = __next_line_color__ (); |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
300 endif |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
301 |
8257 | 302 tmp(++idx) = line (x(:, i), y(:, i), z(:, i), "keylabel", key, |
303 "color", color, | |
304 "linestyle", options.linestyle, | |
305 "marker", options.marker, properties{:}); | |
8078
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
306 endfor |
6257 | 307 endif |
308 | |
309 set (gca (), "view", [-37.5, 30]); | |
5837 | 310 |
6302 | 311 if (nargout > 0 && idx > 0) |
312 retval = tmp; | |
313 endif | |
314 | |
5837 | 315 endfunction |
7245 | 316 |
317 %!demo | |
318 %! z = [0:0.05:5]; | |
319 %! plot3 (cos(2*pi*z), sin(2*pi*z), z, ";helix;"); | |
320 %! plot3 (z, exp(2i*pi*z), ";complex sinusoid;"); |