Mercurial > hg > octave-lyh
annotate scripts/plot/plot3.m @ 8078:4665276ff7f6
correctly plot matrices in plot3
author | David Bateman <dbateman@free.fr> |
---|---|
date | Tue, 02 Sep 2008 14:50:08 -0400 |
parents | a028a5960e18 |
children | 79c874fe5100 |
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 | |
6895 | 82 ## @seealso{plot} |
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 |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
185 hg = hggroup (); |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
186 tmp(++idx) = hg; |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
187 properties = __add_datasource__ ("plot3", hg, {"x", "y", "z"}, properties{:}); |
6257 | 188 |
8078
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
189 hline = line (x(:, i), y(:, i), z(:, i), "keylabel", key, |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
190 "color", color, |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
191 "linestyle", options.linestyle, |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
192 "marker", options.marker, "parent", hg); |
8075
a028a5960e18
Fix for hold with no figures/axes. Set prop/val pairs to hggroups rather than underlying objects. Fix for equality test in array_property
David Bateman <dbateman@free.fr>
parents:
8070
diff
changeset
|
193 |
8078
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
194 __add_line_series__ (hline, hg); |
8075
a028a5960e18
Fix for hold with no figures/axes. Set prop/val pairs to hggroups rather than underlying objects. Fix for equality test in array_property
David Bateman <dbateman@free.fr>
parents:
8070
diff
changeset
|
195 |
8078
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
196 if (! isempty (properties)) |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
197 set (hg, properties{:}); |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
198 endif |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
199 endfor |
6004 | 200 |
6257 | 201 x_set = 0; |
202 y_set = 0; | |
203 z_set = 0; | |
6459 | 204 fmt_set = 0; |
205 properties = {}; | |
6257 | 206 elseif (! x_set) |
207 x = new; | |
208 x_set = 1; | |
209 elseif (! y_set) | |
210 y = new; | |
211 y_set = 1; | |
212 elseif (! z_set) | |
213 z = new; | |
214 z_set = 1; | |
215 else | |
216 if (isvector (x) && isvector (y)) | |
217 if (isvector (z)) | |
218 x = x(:); | |
219 y = y(:); | |
220 z = z(:); | |
221 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
|
222 [x, y] = meshgrid (x, y); |
6257 | 223 else |
8078
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
224 error ("plot3: [length(x), length(y)] must match size(z)"); |
6257 | 225 endif |
226 endif | |
227 | |
7292 | 228 if (! size_equal (x, y, z)) |
6257 | 229 error ("plot3: x, y, and z must have the same shape"); |
230 endif | |
231 | |
6459 | 232 options = __default_plot_options__ (); |
233 key = options.key; | |
234 if (! isempty (key)) | |
235 set (gca (), "key", "on"); | |
236 endif | |
8078
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
237 |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
238 for i = 1 : columns (x) |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
239 color = options.color; |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
240 if (isempty (color)) |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
241 color = __next_line_color__ (); |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
242 endif |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
243 |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
244 hg = hggroup (); |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
245 tmp(++idx) = hg; |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
246 properties = __add_datasource__ ("plot3", hg, {"x", "y", "z"}, properties{:}); |
6459 | 247 |
8078
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
248 hline = line (x(:, i), y(:, i), z(:, i), "keylabel", key, |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
249 "color", color, |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
250 "linestyle", options.linestyle, |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
251 "marker", options.marker, "parent", hg); |
8070
3b53b25e2550
Add data sources and line series
David Bateman <dbateman@free.fr>
parents:
7292
diff
changeset
|
252 |
8078
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
253 __add_line_series__ (hline, hg); |
8070
3b53b25e2550
Add data sources and line series
David Bateman <dbateman@free.fr>
parents:
7292
diff
changeset
|
254 |
8078
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
255 if (! isempty (properties)) |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
256 set (hg, properties{:}); |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
257 endif |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
258 endfor |
6257 | 259 |
260 x = new; | |
261 y_set = 0; | |
262 z_set = 0; | |
6459 | 263 fmt_set = 0; |
264 properties = {}; | |
5837 | 265 endif |
6257 | 266 |
6459 | 267 endwhile |
268 | |
269 if (property_set) | |
270 error ("plot3: properties must appear followed by a value"); | |
271 endif | |
6257 | 272 |
273 ## Handle last plot. | |
274 | |
275 if (x_set) | |
276 if (y_set) | |
277 if (! z_set) | |
278 z = imag (y); | |
279 y = real (y); | |
280 z_set = 1; | |
281 endif | |
282 else | |
283 z = imag (x); | |
284 y = real (x); | |
285 y_set = 1; | |
286 z_set = 1; | |
287 if (rows (x) > 1) | |
288 x = repmat ((1:rows (x))', 1, columns(x)); | |
289 else | |
290 x = 1:columns(x); | |
291 endif | |
5837 | 292 endif |
6257 | 293 |
294 if (isvector (x) && isvector (y)) | |
295 if (isvector (z)) | |
296 x = x(:); | |
297 y = y(:); | |
298 z = z(:); | |
299 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
|
300 [x, y] = meshgrid (x, y); |
6257 | 301 else |
8078
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
302 error ("plot3: [length(x), length(y)] must match size(z)"); |
6257 | 303 endif |
304 endif | |
305 | |
7292 | 306 if (! size_equal (x, y, z)) |
6257 | 307 error ("plot3: x, y, and z must have the same shape"); |
308 endif | |
309 | |
6459 | 310 options = __default_plot_options__ (); |
311 key = options.key; | |
312 if (! isempty (key)) | |
313 set (gca (), "key", "on"); | |
314 endif | |
8078
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
315 |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
316 for i = 1 : columns (x) |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
317 color = options.color; |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
318 if (isempty (color)) |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
319 color = __next_line_color__ (); |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
320 endif |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
321 |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
322 hg = hggroup (); |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
323 tmp(++idx) = hg; |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
324 properties = __add_datasource__ ("plot3", hg, {"x", "y", "z"}, properties{:}); |
6257 | 325 |
8078
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
326 hline = line (x(:, i), y(:, i), z(:, i), "keylabel", key, |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
327 "color", color, |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
328 "linestyle", options.linestyle, |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
329 "marker", options.marker, "parent", hg); |
8070
3b53b25e2550
Add data sources and line series
David Bateman <dbateman@free.fr>
parents:
7292
diff
changeset
|
330 |
8078
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
331 __add_line_series__ (hline, hg); |
8070
3b53b25e2550
Add data sources and line series
David Bateman <dbateman@free.fr>
parents:
7292
diff
changeset
|
332 |
8078
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
333 if (! isempty (properties)) |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
334 set (hg, properties{:}); |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
335 endif |
4665276ff7f6
correctly plot matrices in plot3
David Bateman <dbateman@free.fr>
parents:
8075
diff
changeset
|
336 endfor |
6257 | 337 endif |
338 | |
339 set (gca (), "view", [-37.5, 30]); | |
5837 | 340 |
6302 | 341 if (nargout > 0 && idx > 0) |
342 retval = tmp; | |
343 endif | |
344 | |
5837 | 345 endfunction |
7245 | 346 |
347 %!demo | |
348 %! z = [0:0.05:5]; | |
349 %! plot3 (cos(2*pi*z), sin(2*pi*z), z, ";helix;"); | |
350 %! plot3 (z, exp(2i*pi*z), ";complex sinusoid;"); |