Mercurial > hg > octave-lyh
annotate scripts/plot/plot3.m @ 11523:fd0a3ac60b0e
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 14 Jan 2011 05:47:45 -0500 |
parents | e81914f3921f |
children | c792872f8942 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 1996-2011 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 |
9040
dbd0c77e575e
Cleanup documentation file plot.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
30 ## be plotted in three dimensions. If all arguments are vectors of the |
dbd0c77e575e
Cleanup documentation file plot.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
31 ## same length, then a single continuous line is drawn. If all arguments |
6895 | 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 = {}; | |
10995
e81914f3921f
Update legend code to support fltk (fixes #29348 and partially fixes #30461)
David Bateman <dbateman@free.fr>
parents:
10549
diff
changeset
|
98 tlgnd = {}; |
e81914f3921f
Update legend code to support fltk (fixes #29348 and partially fixes #30461)
David Bateman <dbateman@free.fr>
parents:
10549
diff
changeset
|
99 hlgnd = []; |
6302 | 100 idx = 0; |
101 | |
6257 | 102 ## Gather arguments, decode format, and plot lines. |
6459 | 103 arg = 0; |
104 while (arg++ < nargin) | |
6257 | 105 new = varargin{arg}; |
6459 | 106 new_cell = varargin(arg); |
107 | |
108 if (property_set) | |
109 properties = [properties, new_cell]; | |
110 property_set = 0; | |
111 continue; | |
112 endif | |
6004 | 113 |
6257 | 114 if (ischar (new)) |
115 if (! z_set) | |
10549 | 116 if (! y_set) |
117 if (! x_set) | |
118 error ("plot3: needs x, [ y, [ z ] ]"); | |
119 else | |
120 z = imag (x); | |
121 y = real (x); | |
122 y_set = 1; | |
123 z_set = 1; | |
124 if (rows(x) > 1) | |
125 x = repmat ((1:rows(x))', 1, columns(x)); | |
126 else | |
127 x = 1:columns(x); | |
128 endif | |
129 endif | |
130 else | |
131 z = imag (y); | |
132 y = real (y); | |
133 z_set = 1; | |
134 endif | |
5837 | 135 endif |
6459 | 136 |
137 if (! fmt_set) | |
10549 | 138 [options, valid] = __pltopt__ ("plot3", new, false); |
139 if (! valid) | |
140 properties = [properties, new_cell]; | |
141 property_set = 1; | |
142 continue; | |
143 else | |
144 fmt_set = 1; | |
145 while (arg < nargin && ischar (varargin{arg+1})) | |
146 if (nargin - arg < 2) | |
147 error ("plot3: properties must appear followed by a value"); | |
148 endif | |
149 properties = [properties, varargin(arg+1:arg+2)]; | |
150 arg += 2; | |
151 endwhile | |
152 endif | |
6459 | 153 else |
10549 | 154 properties = [properties, new_cell]; |
155 property_set = 1; | |
156 continue; | |
6459 | 157 endif |
6004 | 158 |
159 if (isvector (x) && isvector (y)) | |
10549 | 160 if (isvector (z)) |
161 x = x(:); | |
162 y = y(:); | |
163 z = z(:); | |
164 elseif (length (x) == rows (z) && length (y) == columns (z)) | |
165 [x, y] = meshgrid (x, y); | |
166 else | |
167 error ("plot3: [length(x), length(y)] must match size(z)"); | |
168 endif | |
6004 | 169 endif |
170 | |
7292 | 171 if (! size_equal (x, y, z)) |
10549 | 172 error ("plot3: x, y, and z must have the same shape"); |
6004 | 173 endif |
174 | |
8078
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
175 for i = 1 : columns (x) |
10135
4516a0c97ced
Handle linestyleorder. Remove @ markers. Treat edgecolor, markeredgecolor and markerfacecolor correctly in scatter.
David Bateman <dbateman@free.fr>
parents:
9385
diff
changeset
|
176 linestyle = options.linestyle; |
4516a0c97ced
Handle linestyleorder. Remove @ markers. Treat edgecolor, markeredgecolor and markerfacecolor correctly in scatter.
David Bateman <dbateman@free.fr>
parents:
9385
diff
changeset
|
177 marker = options.marker; |
10549 | 178 if (isempty (marker) && isempty (linestyle)) |
179 [linestyle, marker] = __next_line_style__ (); | |
180 endif | |
181 color = options.color; | |
10995
e81914f3921f
Update legend code to support fltk (fixes #29348 and partially fixes #30461)
David Bateman <dbateman@free.fr>
parents:
10549
diff
changeset
|
182 if (isempty (color)) |
10549 | 183 color = __next_line_color__ (); |
184 endif | |
8078
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
185 |
10995
e81914f3921f
Update legend code to support fltk (fixes #29348 and partially fixes #30461)
David Bateman <dbateman@free.fr>
parents:
10549
diff
changeset
|
186 tmp(++idx) = line (x(:, i), y(:, i), z(:, i), |
10549 | 187 "color", color, "linestyle", linestyle, |
188 "marker", marker, properties{:}); | |
10995
e81914f3921f
Update legend code to support fltk (fixes #29348 and partially fixes #30461)
David Bateman <dbateman@free.fr>
parents:
10549
diff
changeset
|
189 key = options.key; |
e81914f3921f
Update legend code to support fltk (fixes #29348 and partially fixes #30461)
David Bateman <dbateman@free.fr>
parents:
10549
diff
changeset
|
190 if (! isempty (key)) |
e81914f3921f
Update legend code to support fltk (fixes #29348 and partially fixes #30461)
David Bateman <dbateman@free.fr>
parents:
10549
diff
changeset
|
191 hlgnd = [hlgnd, tmp(idx)]; |
e81914f3921f
Update legend code to support fltk (fixes #29348 and partially fixes #30461)
David Bateman <dbateman@free.fr>
parents:
10549
diff
changeset
|
192 tlgnd = {tlgnd{:}, key}; |
e81914f3921f
Update legend code to support fltk (fixes #29348 and partially fixes #30461)
David Bateman <dbateman@free.fr>
parents:
10549
diff
changeset
|
193 endif |
8078
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
194 endfor |
6004 | 195 |
6257 | 196 x_set = 0; |
197 y_set = 0; | |
198 z_set = 0; | |
6459 | 199 fmt_set = 0; |
200 properties = {}; | |
6257 | 201 elseif (! x_set) |
202 x = new; | |
203 x_set = 1; | |
204 elseif (! y_set) | |
205 y = new; | |
206 y_set = 1; | |
207 elseif (! z_set) | |
208 z = new; | |
209 z_set = 1; | |
210 else | |
211 if (isvector (x) && isvector (y)) | |
10549 | 212 if (isvector (z)) |
213 x = x(:); | |
214 y = y(:); | |
215 z = z(:); | |
216 elseif (length (x) == rows (z) && length (y) == columns (z)) | |
217 [x, y] = meshgrid (x, y); | |
218 else | |
219 error ("plot3: [length(x), length(y)] must match size(z)"); | |
220 endif | |
6257 | 221 endif |
222 | |
7292 | 223 if (! size_equal (x, y, z)) |
10549 | 224 error ("plot3: x, y, and z must have the same shape"); |
6257 | 225 endif |
226 | |
6459 | 227 options = __default_plot_options__ (); |
8078
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
228 for i = 1 : columns (x) |
10135
4516a0c97ced
Handle linestyleorder. Remove @ markers. Treat edgecolor, markeredgecolor and markerfacecolor correctly in scatter.
David Bateman <dbateman@free.fr>
parents:
9385
diff
changeset
|
229 linestyle = options.linestyle; |
4516a0c97ced
Handle linestyleorder. Remove @ markers. Treat edgecolor, markeredgecolor and markerfacecolor correctly in scatter.
David Bateman <dbateman@free.fr>
parents:
9385
diff
changeset
|
230 marker = options.marker; |
10549 | 231 if (isempty (marker) && isempty (linestyle)) |
232 [linestyle, marker] = __next_line_style__ (); | |
233 endif | |
234 color = options.color; | |
235 if (isempty (color)) | |
236 color = __next_line_color__ (); | |
237 endif | |
8078
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
238 |
10995
e81914f3921f
Update legend code to support fltk (fixes #29348 and partially fixes #30461)
David Bateman <dbateman@free.fr>
parents:
10549
diff
changeset
|
239 tmp(++idx) = line (x(:, i), y(:, i), z(:, i), |
10549 | 240 "color", color, "linestyle", linestyle, |
241 "marker", marker, properties{:}); | |
10995
e81914f3921f
Update legend code to support fltk (fixes #29348 and partially fixes #30461)
David Bateman <dbateman@free.fr>
parents:
10549
diff
changeset
|
242 key = options.key; |
e81914f3921f
Update legend code to support fltk (fixes #29348 and partially fixes #30461)
David Bateman <dbateman@free.fr>
parents:
10549
diff
changeset
|
243 if (! isempty (key)) |
e81914f3921f
Update legend code to support fltk (fixes #29348 and partially fixes #30461)
David Bateman <dbateman@free.fr>
parents:
10549
diff
changeset
|
244 hlgnd = [hlgnd, tmp(idx)]; |
e81914f3921f
Update legend code to support fltk (fixes #29348 and partially fixes #30461)
David Bateman <dbateman@free.fr>
parents:
10549
diff
changeset
|
245 tlgnd = {tlgnd{:}, key}; |
e81914f3921f
Update legend code to support fltk (fixes #29348 and partially fixes #30461)
David Bateman <dbateman@free.fr>
parents:
10549
diff
changeset
|
246 endif |
8078
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
247 endfor |
6257 | 248 |
249 x = new; | |
250 y_set = 0; | |
251 z_set = 0; | |
6459 | 252 fmt_set = 0; |
253 properties = {}; | |
5837 | 254 endif |
6257 | 255 |
6459 | 256 endwhile |
257 | |
258 if (property_set) | |
259 error ("plot3: properties must appear followed by a value"); | |
260 endif | |
6257 | 261 |
262 ## Handle last plot. | |
263 | |
264 if (x_set) | |
265 if (y_set) | |
266 if (! z_set) | |
10549 | 267 z = imag (y); |
268 y = real (y); | |
269 z_set = 1; | |
6257 | 270 endif |
271 else | |
272 z = imag (x); | |
273 y = real (x); | |
274 y_set = 1; | |
275 z_set = 1; | |
276 if (rows (x) > 1) | |
10549 | 277 x = repmat ((1:rows (x))', 1, columns(x)); |
6257 | 278 else |
10549 | 279 x = 1:columns(x); |
6257 | 280 endif |
5837 | 281 endif |
6257 | 282 |
283 if (isvector (x) && isvector (y)) | |
284 if (isvector (z)) | |
10549 | 285 x = x(:); |
286 y = y(:); | |
287 z = z(:); | |
6257 | 288 elseif (length (x) == rows (z) && length (y) == columns (z)) |
10549 | 289 [x, y] = meshgrid (x, y); |
6257 | 290 else |
10549 | 291 error ("plot3: [length(x), length(y)] must match size(z)"); |
6257 | 292 endif |
293 endif | |
294 | |
7292 | 295 if (! size_equal (x, y, z)) |
6257 | 296 error ("plot3: x, y, and z must have the same shape"); |
297 endif | |
298 | |
6459 | 299 options = __default_plot_options__ (); |
8078
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
300 |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
301 for i = 1 : columns (x) |
10135
4516a0c97ced
Handle linestyleorder. Remove @ markers. Treat edgecolor, markeredgecolor and markerfacecolor correctly in scatter.
David Bateman <dbateman@free.fr>
parents:
9385
diff
changeset
|
302 linestyle = options.linestyle; |
4516a0c97ced
Handle linestyleorder. Remove @ markers. Treat edgecolor, markeredgecolor and markerfacecolor correctly in scatter.
David Bateman <dbateman@free.fr>
parents:
9385
diff
changeset
|
303 marker = options.marker; |
4516a0c97ced
Handle linestyleorder. Remove @ markers. Treat edgecolor, markeredgecolor and markerfacecolor correctly in scatter.
David Bateman <dbateman@free.fr>
parents:
9385
diff
changeset
|
304 if (isempty (marker) && isempty (linestyle)) |
10549 | 305 [linestyle, marker] = __next_line_style__ (); |
10135
4516a0c97ced
Handle linestyleorder. Remove @ markers. Treat edgecolor, markeredgecolor and markerfacecolor correctly in scatter.
David Bateman <dbateman@free.fr>
parents:
9385
diff
changeset
|
306 endif |
8078
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
307 color = options.color; |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
308 if (isempty (color)) |
10549 | 309 color = __next_line_color__ (); |
8078
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
310 endif |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
311 |
10995
e81914f3921f
Update legend code to support fltk (fixes #29348 and partially fixes #30461)
David Bateman <dbateman@free.fr>
parents:
10549
diff
changeset
|
312 tmp(++idx) = line (x(:, i), y(:, i), z(:, i), |
10549 | 313 "color", color, "linestyle", linestyle, |
314 "marker", marker, properties{:}); | |
10995
e81914f3921f
Update legend code to support fltk (fixes #29348 and partially fixes #30461)
David Bateman <dbateman@free.fr>
parents:
10549
diff
changeset
|
315 key = options.key; |
e81914f3921f
Update legend code to support fltk (fixes #29348 and partially fixes #30461)
David Bateman <dbateman@free.fr>
parents:
10549
diff
changeset
|
316 if (! isempty (key)) |
e81914f3921f
Update legend code to support fltk (fixes #29348 and partially fixes #30461)
David Bateman <dbateman@free.fr>
parents:
10549
diff
changeset
|
317 hlgnd = [hlgnd, tmp(idx)]; |
e81914f3921f
Update legend code to support fltk (fixes #29348 and partially fixes #30461)
David Bateman <dbateman@free.fr>
parents:
10549
diff
changeset
|
318 tlgnd = {tlgnd{:}, key}; |
e81914f3921f
Update legend code to support fltk (fixes #29348 and partially fixes #30461)
David Bateman <dbateman@free.fr>
parents:
10549
diff
changeset
|
319 endif |
8078
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
320 endfor |
6257 | 321 endif |
322 | |
10995
e81914f3921f
Update legend code to support fltk (fixes #29348 and partially fixes #30461)
David Bateman <dbateman@free.fr>
parents:
10549
diff
changeset
|
323 if (!isempty (hlgnd)) |
e81914f3921f
Update legend code to support fltk (fixes #29348 and partially fixes #30461)
David Bateman <dbateman@free.fr>
parents:
10549
diff
changeset
|
324 legend (gca(), hlgnd, tlgnd); |
e81914f3921f
Update legend code to support fltk (fixes #29348 and partially fixes #30461)
David Bateman <dbateman@free.fr>
parents:
10549
diff
changeset
|
325 endif |
e81914f3921f
Update legend code to support fltk (fixes #29348 and partially fixes #30461)
David Bateman <dbateman@free.fr>
parents:
10549
diff
changeset
|
326 |
6257 | 327 set (gca (), "view", [-37.5, 30]); |
5837 | 328 |
6302 | 329 if (nargout > 0 && idx > 0) |
330 retval = tmp; | |
331 endif | |
332 | |
5837 | 333 endfunction |
7245 | 334 |
335 %!demo | |
336 %! z = [0:0.05:5]; | |
337 %! plot3 (cos(2*pi*z), sin(2*pi*z), z, ";helix;"); | |
338 %! plot3 (z, exp(2i*pi*z), ";complex sinusoid;"); |