1 ## Copyright (C) 1996-2012 John W. Eaton
3 ## This file is part of Octave.
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 3 of the License, or (at
8 ## your option) any later version.
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.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {} plot3 (@var{args})
21 ## Produce three-dimensional plots. Many different combinations of
22 ## arguments are possible. The simplest form is
25 ## plot3 (@var{x}, @var{y}, @var{z})
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
33 ## separate line. No attempt is made to transpose the arguments to make
34 ## the number of rows match.
36 ## If only two arguments are given, as
39 ## plot3 (@var{x}, @var{c})
43 ## the real and imaginary parts of the second argument are used
44 ## as the @var{y} and @var{z} coordinates, respectively.
46 ## If only one argument is given, as
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.
56 ## Arguments may also be given in groups of three as
59 ## plot3 (@var{x1}, @var{y1}, @var{z1}, @var{x2}, @var{y2}, @var{z2}, @dots{})
63 ## in which each set of three arguments is treated as a separate line or
64 ## set of lines in three dimensions.
66 ## To plot multiple one- or two-argument groups, separate each group
67 ## with an empty format string, as
70 ## plot3 (@var{x1}, @var{c1}, "", @var{c2}, "", @dots{})
73 ## An example of the use of @code{plot3} is
78 ## plot3 (cos (2*pi*z), sin (2*pi*z), z, ";helix;");
79 ## plot3 (z, exp (2i*pi*z), ";complex sinusoid;");
82 ## @seealso{plot, xlabel, ylabel, zlabel, title, print}
85 ## Author: Paul Kienzle
86 ## (modified from __plt__.m)
88 function retval = plot3 (varargin)
102 ## Gather arguments, decode format, and plot lines.
104 while (arg++ < nargin)
106 new_cell = varargin(arg);
109 properties = [properties, new_cell];
118 error ("plot3: needs x, [ y, [ z ] ]");
125 x = repmat ((1:rows(x))', 1, columns(x));
138 [options, valid] = __pltopt__ ("plot3", new, false);
140 properties = [properties, new_cell];
145 while (arg < nargin && ischar (varargin{arg+1}))
146 if (nargin - arg < 2)
147 error ("plot3: properties must appear followed by a value");
149 properties = [properties, varargin(arg+1:arg+2)];
154 properties = [properties, new_cell];
159 if (isvector (x) && isvector (y))
164 elseif (length (x) == rows (z) && length (y) == columns (z))
165 [x, y] = meshgrid (x, y);
167 error ("plot3: [length(x), length(y)] must match size(z)");
171 if (! size_equal (x, y, z))
172 error ("plot3: x, y, and z must have the same shape");
173 elseif (ndims (x) > 2)
174 error ("plot3: x, y, and z must not have more than two dimensions");
177 for i = 1 : columns (x)
178 linestyle = options.linestyle;
179 marker = options.marker;
180 if (isempty (marker) && isempty (linestyle))
181 [linestyle, marker] = __next_line_style__ ();
183 color = options.color;
185 color = __next_line_color__ ();
188 tmp(++idx) = line (x(:, i), y(:, i), z(:, i),
189 "color", color, "linestyle", linestyle,
190 "marker", marker, properties{:});
193 hlgnd = [hlgnd, tmp(idx)];
194 tlgnd = {tlgnd{:}, key};
213 if (isvector (x) && isvector (y))
218 elseif (length (x) == rows (z) && length (y) == columns (z))
219 [x, y] = meshgrid (x, y);
221 error ("plot3: [length(x), length(y)] must match size(z)");
225 if (! size_equal (x, y, z))
226 error ("plot3: x, y, and z must have the same shape");
227 elseif (ndims (x) > 2)
228 error ("plot3: x, y, and z must not have more than two dimensions");
231 options = __default_plot_options__ ();
232 for i = 1 : columns (x)
233 linestyle = options.linestyle;
234 marker = options.marker;
235 if (isempty (marker) && isempty (linestyle))
236 [linestyle, marker] = __next_line_style__ ();
238 color = options.color;
240 color = __next_line_color__ ();
243 tmp(++idx) = line (x(:, i), y(:, i), z(:, i),
244 "color", color, "linestyle", linestyle,
245 "marker", marker, properties{:});
248 hlgnd = [hlgnd, tmp(idx)];
249 tlgnd = {tlgnd{:}, key};
263 error ("plot3: properties must appear followed by a value");
281 x = repmat ((1:rows (x))', 1, columns(x));
287 if (isvector (x) && isvector (y))
292 elseif (length (x) == rows (z) && length (y) == columns (z))
293 [x, y] = meshgrid (x, y);
295 error ("plot3: [length(x), length(y)] must match size(z)");
299 if (! size_equal (x, y, z))
300 error ("plot3: x, y, and z must have the same shape");
301 elseif (ndims (x) > 2)
302 error ("plot3: x, y, and z must not have more than two dimensions");
305 options = __default_plot_options__ ();
307 for i = 1 : columns (x)
308 linestyle = options.linestyle;
309 marker = options.marker;
310 if (isempty (marker) && isempty (linestyle))
311 [linestyle, marker] = __next_line_style__ ();
313 color = options.color;
315 color = __next_line_color__ ();
318 tmp(++idx) = line (x(:, i), y(:, i), z(:, i),
319 "color", color, "linestyle", linestyle,
320 "marker", marker, properties{:});
323 hlgnd = [hlgnd, tmp(idx)];
324 tlgnd = {tlgnd{:}, key};
329 if (!isempty (hlgnd))
330 legend (gca(), hlgnd, tlgnd);
333 set (gca (), "view", [-37.5, 30]);
335 if (nargout > 0 && idx > 0)
344 %! plot3 (cos(2*pi*z), sin(2*pi*z), z, ";helix;");
345 %! plot3 (z, exp(2i*pi*z), ";complex sinusoid;");