Mercurial > hg > octave-lyh
annotate scripts/plot/plot3.m @ 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
author | David Bateman <dbateman@free.fr> |
---|---|
date | Fri, 29 Aug 2008 15:48:44 -0400 |
parents | 3b53b25e2550 |
children | 4665276ff7f6 |
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)) | |
164 error ("plot3: [length(x), length(y)] must match size(z)"); | |
165 else | |
166 [x, y] = meshgrid (x, y); | |
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 | |
6459 | 178 color = options.color; |
179 if (isempty (options.color)) | |
180 color = __next_line_color__ (); | |
181 endif | |
6257 | 182 |
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
|
183 hg = hggroup (); |
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
|
184 tmp(++idx) = hg; |
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
|
185 properties = __add_datasource__ ("plot3", hg, {"x", "y", "z"}, properties{:}); |
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
|
186 |
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
|
187 hline = line (x(:), y(:), z(:), "keylabel", key, "color", color, |
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
|
188 "linestyle", options.linestyle, |
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
|
189 "marker", options.marker, "parent", hg); |
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
|
190 |
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
|
191 __add_line_series__ (hline, hg); |
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
|
192 |
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 set (hg, properties{:}); |
6004 | 194 |
6257 | 195 x_set = 0; |
196 y_set = 0; | |
197 z_set = 0; | |
6459 | 198 fmt_set = 0; |
199 properties = {}; | |
6257 | 200 elseif (! x_set) |
201 x = new; | |
202 x_set = 1; | |
203 elseif (! y_set) | |
204 y = new; | |
205 y_set = 1; | |
206 elseif (! z_set) | |
207 z = new; | |
208 z_set = 1; | |
209 else | |
210 if (isvector (x) && isvector (y)) | |
211 if (isvector (z)) | |
212 x = x(:); | |
213 y = y(:); | |
214 z = z(:); | |
215 elseif (length (x) == rows (z) && length (y) == columns (z)) | |
216 error ("plot3: [length(x), length(y)] must match size(z)"); | |
217 else | |
218 [x, y] = meshgrid (x, y); | |
219 endif | |
220 endif | |
221 | |
7292 | 222 if (! size_equal (x, y, z)) |
6257 | 223 error ("plot3: x, y, and z must have the same shape"); |
224 endif | |
225 | |
6459 | 226 options = __default_plot_options__ (); |
227 key = options.key; | |
228 if (! isempty (key)) | |
229 set (gca (), "key", "on"); | |
230 endif | |
231 color = options.color; | |
232 if (isempty (color)) | |
233 color = __next_line_color__ (); | |
234 endif | |
235 | |
8070
3b53b25e2550
Add data sources and line series
David Bateman <dbateman@free.fr>
parents:
7292
diff
changeset
|
236 hg = hggroup (); |
3b53b25e2550
Add data sources and line series
David Bateman <dbateman@free.fr>
parents:
7292
diff
changeset
|
237 tmp(++idx) = hg; |
3b53b25e2550
Add data sources and line series
David Bateman <dbateman@free.fr>
parents:
7292
diff
changeset
|
238 properties = __add_datasource__ ("plot3", hg, {"x", "y", "z"}, properties{:}); |
3b53b25e2550
Add data sources and line series
David Bateman <dbateman@free.fr>
parents:
7292
diff
changeset
|
239 |
3b53b25e2550
Add data sources and line series
David Bateman <dbateman@free.fr>
parents:
7292
diff
changeset
|
240 hline = line (x(:), y(:), z(:), "keylabel", key, "color", color, |
3b53b25e2550
Add data sources and line series
David Bateman <dbateman@free.fr>
parents:
7292
diff
changeset
|
241 "linestyle", options.linestyle, |
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
|
242 "marker", options.marker, "parent", hg); |
8070
3b53b25e2550
Add data sources and line series
David Bateman <dbateman@free.fr>
parents:
7292
diff
changeset
|
243 |
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
|
244 __add_line_series__ (hline, hg); |
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
|
245 |
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
|
246 set (hg, properties{:}); |
6257 | 247 |
248 x = new; | |
249 y_set = 0; | |
250 z_set = 0; | |
6459 | 251 fmt_set = 0; |
252 properties = {}; | |
5837 | 253 endif |
6257 | 254 |
6459 | 255 endwhile |
256 | |
257 if (property_set) | |
258 error ("plot3: properties must appear followed by a value"); | |
259 endif | |
6257 | 260 |
261 ## Handle last plot. | |
262 | |
263 if (x_set) | |
264 if (y_set) | |
265 if (! z_set) | |
266 z = imag (y); | |
267 y = real (y); | |
268 z_set = 1; | |
269 endif | |
270 else | |
271 z = imag (x); | |
272 y = real (x); | |
273 y_set = 1; | |
274 z_set = 1; | |
275 if (rows (x) > 1) | |
276 x = repmat ((1:rows (x))', 1, columns(x)); | |
277 else | |
278 x = 1:columns(x); | |
279 endif | |
5837 | 280 endif |
6257 | 281 |
282 if (isvector (x) && isvector (y)) | |
283 if (isvector (z)) | |
284 x = x(:); | |
285 y = y(:); | |
286 z = z(:); | |
287 elseif (length (x) == rows (z) && length (y) == columns (z)) | |
288 error ("plot3: [length(x), length(y)] must match size(z)"); | |
289 else | |
290 [x, y] = meshgrid (x, y); | |
291 endif | |
292 endif | |
293 | |
7292 | 294 if (! size_equal (x, y, z)) |
6257 | 295 error ("plot3: x, y, and z must have the same shape"); |
296 endif | |
297 | |
6459 | 298 options = __default_plot_options__ (); |
299 key = options.key; | |
300 if (! isempty (key)) | |
301 set (gca (), "key", "on"); | |
302 endif | |
303 color = options.color; | |
304 if (isempty (color)) | |
305 color = __next_line_color__ (); | |
306 endif | |
6257 | 307 |
8070
3b53b25e2550
Add data sources and line series
David Bateman <dbateman@free.fr>
parents:
7292
diff
changeset
|
308 hg = hggroup (); |
3b53b25e2550
Add data sources and line series
David Bateman <dbateman@free.fr>
parents:
7292
diff
changeset
|
309 tmp(++idx) = hg; |
3b53b25e2550
Add data sources and line series
David Bateman <dbateman@free.fr>
parents:
7292
diff
changeset
|
310 properties = __add_datasource__ ("plot3", hg, {"x", "y", "z"}, properties{:}); |
3b53b25e2550
Add data sources and line series
David Bateman <dbateman@free.fr>
parents:
7292
diff
changeset
|
311 |
3b53b25e2550
Add data sources and line series
David Bateman <dbateman@free.fr>
parents:
7292
diff
changeset
|
312 hline = line (x(:), y(:), z(:), "keylabel", key, "color", color, |
3b53b25e2550
Add data sources and line series
David Bateman <dbateman@free.fr>
parents:
7292
diff
changeset
|
313 "linestyle", options.linestyle, |
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
|
314 "marker", options.marker, "parent", hg); |
8070
3b53b25e2550
Add data sources and line series
David Bateman <dbateman@free.fr>
parents:
7292
diff
changeset
|
315 |
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
|
316 __add_line_series__ (hline, hg); |
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
|
317 |
6257 | 318 endif |
319 | |
320 set (gca (), "view", [-37.5, 30]); | |
5837 | 321 |
6302 | 322 if (nargout > 0 && idx > 0) |
323 retval = tmp; | |
324 endif | |
325 | |
5837 | 326 endfunction |
7245 | 327 |
328 %!demo | |
329 %! z = [0:0.05:5]; | |
330 %! plot3 (cos(2*pi*z), sin(2*pi*z), z, ";helix;"); | |
331 %! plot3 (z, exp(2i*pi*z), ";complex sinusoid;"); |