Mercurial > hg > octave-lyh
annotate scripts/plot/private/__scatter__.m @ 10083:614ad9e7a17b
use patches again in scatter plots
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Mon, 11 Jan 2010 22:06:19 +0100 |
parents | 28af9aa6f9ed |
children | 4516a0c97ced |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2007, 2008, 2009 David Bateman |
7189 | 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 | |
7 ## the Free Software Foundation; either version 3 of the License, or (at | |
8 ## your option) any later version. | |
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 | |
16 ## along with Octave; see the file COPYING. If not, see | |
17 ## <http://www.gnu.org/licenses/>. | |
18 | |
8812
7d48766c21a5
use consistent format for doc strings of internal functions
John W. Eaton <jwe@octave.org>
parents:
8257
diff
changeset
|
19 ## -*- texinfo -*- |
7d48766c21a5
use consistent format for doc strings of internal functions
John W. Eaton <jwe@octave.org>
parents:
8257
diff
changeset
|
20 ## @deftypefn {Function File} {@var{hg} =} __scatter__ (@dots{}) |
7189 | 21 ## Undocumented internal function. |
8812
7d48766c21a5
use consistent format for doc strings of internal functions
John W. Eaton <jwe@octave.org>
parents:
8257
diff
changeset
|
22 ## @end deftypefn |
7189 | 23 |
8257 | 24 function hg = __scatter__ (varargin) |
7191 | 25 |
26 h = varargin{1}; | |
27 nd = varargin{2}; | |
28 fcn = varargin{3}; | |
29 x = varargin{4}(:); | |
30 y = varargin{5}(:); | |
7189 | 31 istart = 6; |
7191 | 32 |
7189 | 33 if (nd == 3) |
7191 | 34 z = varargin{6}(:); |
7421 | 35 idx = isnan(x) | isnan (y) | isnan (z); |
36 x (idx) = []; | |
37 y (idx) = []; | |
38 z (idx) = []; | |
7189 | 39 istart = 7; |
40 else | |
7421 | 41 idx = isnan(x) | isnan (y); |
42 x (idx) = []; | |
43 y (idx) = []; | |
7191 | 44 z = zeros (length (x), 0); |
7189 | 45 endif |
46 | |
47 firstnonnumeric = Inf; | |
48 for i = istart:nargin | |
7191 | 49 if (! isnumeric (varargin{i})) |
7189 | 50 firstnonnumeric = i; |
51 break; | |
52 endif | |
53 endfor | |
54 | |
7235 | 55 if (istart < nargin && firstnonnumeric > istart) |
7191 | 56 s = varargin{istart}; |
7189 | 57 if (isempty (s)) |
7603
689652eb95d1
fix for scatter markersize
David Bateman <dbateman@free.fr>
parents:
7421
diff
changeset
|
58 s = 6; |
7189 | 59 endif |
60 else | |
7603
689652eb95d1
fix for scatter markersize
David Bateman <dbateman@free.fr>
parents:
7421
diff
changeset
|
61 s = 6; |
7189 | 62 endif |
63 | |
64 if (istart < nargin && firstnonnumeric > istart + 1) | |
7191 | 65 c = varargin{istart + 1}; |
7189 | 66 if (isvector (c)) |
10078 | 67 if (columns (c) != 3) |
8257 | 68 c = c(:); |
69 endif | |
9295
10b9a71a81f1
__scatter__.m: If the color spec is empty, set using __next_line_color__.
Ben Abbott <bpabbott@mac.com>
parents:
9085
diff
changeset
|
70 elseif (isempty (c)) |
10078 | 71 c = __next_line_color__(); |
7189 | 72 endif |
7191 | 73 elseif (firstnonnumeric == istart + 1 && ischar (varargin{istart + 1})) |
7189 | 74 c = varargin{istart + 1}; |
75 firstnonnumeric++; | |
76 else | |
10078 | 77 c = __next_line_color__(); |
7189 | 78 endif |
79 | |
80 newargs = {}; | |
81 filled = false; | |
82 have_marker = false; | |
83 marker = "o"; | |
84 iarg = firstnonnumeric; | |
85 while (iarg <= nargin) | |
7191 | 86 arg = varargin{iarg++}; |
8190
73d6b71788c0
use case-insensitive comparison for graphics properties; misc style fixes
John W. Eaton <jwe@octave.org>
parents:
7768
diff
changeset
|
87 if (ischar (arg) && strncmpi (arg, "filled", 6)) |
7189 | 88 filled = true; |
7768
a2d9f325b65a
Use isschar instead of deprecated isstr
Rafael Laboissiere <rafael@debian.org>
parents:
7603
diff
changeset
|
89 elseif ((ischar (arg) || iscell (arg)) && ! have_marker) |
8257 | 90 [linespec, valid] = __pltopt__ (fcn, arg, false); |
7189 | 91 if (valid) |
92 have_marker = true; | |
93 marker = linespec.marker; | |
94 if (strncmp (marker, "none", 4)) | |
95 marker = "o"; | |
96 endif | |
97 else | |
8257 | 98 error ("%s: invalid linespec", fcn); |
7189 | 99 endif |
100 else | |
101 newargs{end+1} = arg; | |
102 if (iarg <= nargin) | |
103 newargs{end+1} = varagin{iarg++}; | |
104 endif | |
105 endif | |
106 endwhile | |
107 | |
8257 | 108 hg = hggroup (); |
109 newargs = __add_datasource__ (fcn, hg, {"x", "y", "z", "c", "size"}, | |
110 newargs{:}); | |
111 | |
112 addproperty ("xdata", hg, "data", x); | |
113 addproperty ("ydata", hg, "data", y); | |
114 addproperty ("zdata", hg, "data", z); | |
10078 | 115 addproperty ("cdata", hg, "data", c); |
116 addproperty ("sizedata", hg, "data", s); | |
8257 | 117 addlistener (hg, "xdata", @update_data); |
118 addlistener (hg, "ydata", @update_data); | |
119 addlistener (hg, "zdata", @update_data); | |
120 addlistener (hg, "cdata", @update_data); | |
121 addlistener (hg, "sizedata", @update_data); | |
122 | |
10083
614ad9e7a17b
use patches again in scatter plots
Jaroslav Hajek <highegg@gmail.com>
parents:
10082
diff
changeset
|
123 if (numel (x) <= 100) |
10078 | 124 |
125 ## For small number of points, we'll construct an object for each point. | |
126 | |
127 if (numel (s) == 1) | |
128 s = repmat (s, numel(x), 1); | |
129 endif | |
130 | |
131 if (ischar (c)) | |
132 for i = 1 : numel (x) | |
10083
614ad9e7a17b
use patches again in scatter plots
Jaroslav Hajek <highegg@gmail.com>
parents:
10082
diff
changeset
|
133 h = __go_patch__ (hg, "xdata", x(i), "ydata", y(i), "zdata", z(i,:), |
614ad9e7a17b
use patches again in scatter plots
Jaroslav Hajek <highegg@gmail.com>
parents:
10082
diff
changeset
|
134 "faces", 1, "vertices", [x(i), y(i), z(i,:)], |
614ad9e7a17b
use patches again in scatter plots
Jaroslav Hajek <highegg@gmail.com>
parents:
10082
diff
changeset
|
135 "facecolor", "none", "edgecolor", c, "marker", marker, |
614ad9e7a17b
use patches again in scatter plots
Jaroslav Hajek <highegg@gmail.com>
parents:
10082
diff
changeset
|
136 "markersize", s(i), "linestyle", "none"); |
10078 | 137 if (filled) |
138 set(h, "markerfacecolor", c); | |
139 endif | |
140 endfor | |
141 else | |
142 if (rows (c) == 1) | |
143 c = repmat (c, numel (x), 1); | |
8257 | 144 endif |
10078 | 145 |
146 for i = 1 : numel (x) | |
10083
614ad9e7a17b
use patches again in scatter plots
Jaroslav Hajek <highegg@gmail.com>
parents:
10082
diff
changeset
|
147 h = __go_patch__ (hg, "xdata", x(i), "ydata", y(i), "zdata", z(i,:), |
614ad9e7a17b
use patches again in scatter plots
Jaroslav Hajek <highegg@gmail.com>
parents:
10082
diff
changeset
|
148 "faces", 1, "vertices", [x(i), y(i), z(i,:)], |
614ad9e7a17b
use patches again in scatter plots
Jaroslav Hajek <highegg@gmail.com>
parents:
10082
diff
changeset
|
149 "facecolor", "none", "edgecolor", "flat", |
614ad9e7a17b
use patches again in scatter plots
Jaroslav Hajek <highegg@gmail.com>
parents:
10082
diff
changeset
|
150 "cdata", c(i,:), |
614ad9e7a17b
use patches again in scatter plots
Jaroslav Hajek <highegg@gmail.com>
parents:
10082
diff
changeset
|
151 "marker", marker, "markersize", s(i), |
614ad9e7a17b
use patches again in scatter plots
Jaroslav Hajek <highegg@gmail.com>
parents:
10082
diff
changeset
|
152 "linestyle", "none"); |
10078 | 153 if (filled) |
10083
614ad9e7a17b
use patches again in scatter plots
Jaroslav Hajek <highegg@gmail.com>
parents:
10082
diff
changeset
|
154 set(h, "markerfacecolor", "flat"); |
10078 | 155 endif |
156 endfor | |
157 endif | |
158 | |
8257 | 159 else |
10078 | 160 |
161 ## For larger numbers of points, we split the points by common color. | |
162 | |
163 vert = [x, y, z]; | |
164 | |
165 if (ischar (c) || rows (c) == 1) | |
166 h = render_size_color (hg, vert, s, c, marker, filled); | |
167 else | |
168 [cc, idx] = unique_idx (c, "rows"); | |
169 if (isscalar (s)) | |
170 for i = 1:rows (x) | |
171 h = render_size_color (hg, vert(idx{i},:), s, cc(i,:), marker, filled); | |
172 endfor | |
173 else | |
174 for i = 1:rows (x) | |
175 h = render_size_color (hg, vert(idx{i},:), s(idx{i}), cc(i,:), marker, filled); | |
176 endfor | |
8257 | 177 endif |
7189 | 178 endif |
10078 | 179 |
180 endif | |
181 | |
182 if (! ischar (c)) | |
183 ax = get (hg, "parent"); | |
184 clim = get (ax, "clim"); | |
185 if (min(c(:)) < clim(1)) | |
186 clim(1) = min(c(:)); | |
187 set (ax, "clim", clim); | |
188 endif | |
189 if (max(c(:)) > clim(2)) | |
190 set (ax, "clim", [clim(1), max(c(:))]); | |
191 endif | |
7189 | 192 endif |
193 | |
8257 | 194 addproperty ("linewidth", hg, "patchlinewidth", 0.5); |
195 addproperty ("marker", hg, "patchmarker", marker); | |
196 if (numel (x) > 0) | |
197 addproperty ("markerfacecolor", hg, "patchmarkerfacecolor", "none"); | |
198 addproperty ("markeredgecolor", hg, "patchmarkeredgecolor", "none"); | |
199 else | |
200 addproperty ("markerfacecolor", hg, "patchmarkerfacecolor", | |
201 get (h, "markerfacecolor")); | |
202 addproperty ("markeredgecolor", hg, "patchmarkeredgecolor", | |
10078 | 203 get (h, "color")); |
8257 | 204 endif |
205 addlistener (hg, "linewidth", @update_props); | |
206 addlistener (hg, "marker", @update_props); | |
207 addlistener (hg, "markerfacecolor", @update_props); | |
208 addlistener (hg, "markeredgecolor", @update_props); | |
209 | |
210 if (! isempty (newargs)) | |
211 set (hg, newargs{:}) | |
212 endif | |
213 | |
7189 | 214 endfunction |
8257 | 215 |
10078 | 216 function [y, idx] = unique_idx (x, byrows) |
217 if (nargin == 2) | |
218 [xx, idx] = sortrows (x); | |
219 n = rows (x); | |
220 jdx = find (any (xx(1:n-1,:) != xx(2:n,:), 2)); | |
10082
28af9aa6f9ed
fix typos in __scatter__
Jaroslav Hajek <highegg@gmail.com>
parents:
10078
diff
changeset
|
221 jdx(end+1) = n; |
10078 | 222 y = xx(jdx,:); |
223 else | |
224 [xx, idx] = sort (x); | |
225 n = length (x); | |
226 jdx = find (xx(1:n-1,:) != xx(2:n,:)); | |
10082
28af9aa6f9ed
fix typos in __scatter__
Jaroslav Hajek <highegg@gmail.com>
parents:
10078
diff
changeset
|
227 jdx(end+1) = n; |
10078 | 228 y = xx(jdx); |
229 endif | |
230 | |
231 if (nargin == 2 || columns (x) == 1) | |
232 idx = mat2cell (idx, diff ([0; jdx]), 1); | |
233 else | |
234 idx = mat2cell (idx, 1, diff ([0, jdx])); | |
235 endif | |
236 endfunction | |
237 | |
238 function h = render_size_color(hg, vert, s, c, marker, filled) | |
239 if (isscalar (s)) | |
240 x = vert(:,1); | |
241 y = vert(:,2); | |
242 z = vert(:,3:end); | |
10083
614ad9e7a17b
use patches again in scatter plots
Jaroslav Hajek <highegg@gmail.com>
parents:
10082
diff
changeset
|
243 if (ischar (c)) |
614ad9e7a17b
use patches again in scatter plots
Jaroslav Hajek <highegg@gmail.com>
parents:
10082
diff
changeset
|
244 h = __go_patch__ (hg, "xdata", x, "ydata", y, "zdata", z, |
614ad9e7a17b
use patches again in scatter plots
Jaroslav Hajek <highegg@gmail.com>
parents:
10082
diff
changeset
|
245 "faces", 1, "vertices", vert, |
614ad9e7a17b
use patches again in scatter plots
Jaroslav Hajek <highegg@gmail.com>
parents:
10082
diff
changeset
|
246 "facecolor", "none", "edgecolor", c, "marker", marker, |
614ad9e7a17b
use patches again in scatter plots
Jaroslav Hajek <highegg@gmail.com>
parents:
10082
diff
changeset
|
247 "markersize", s, "linestyle", "none"); |
614ad9e7a17b
use patches again in scatter plots
Jaroslav Hajek <highegg@gmail.com>
parents:
10082
diff
changeset
|
248 if (filled) |
614ad9e7a17b
use patches again in scatter plots
Jaroslav Hajek <highegg@gmail.com>
parents:
10082
diff
changeset
|
249 set(h, "markerfacecolor", c); |
614ad9e7a17b
use patches again in scatter plots
Jaroslav Hajek <highegg@gmail.com>
parents:
10082
diff
changeset
|
250 endif |
614ad9e7a17b
use patches again in scatter plots
Jaroslav Hajek <highegg@gmail.com>
parents:
10082
diff
changeset
|
251 else |
614ad9e7a17b
use patches again in scatter plots
Jaroslav Hajek <highegg@gmail.com>
parents:
10082
diff
changeset
|
252 h = __go_patch__ (hg, "xdata", x, "ydata", y, "zdata", z, |
614ad9e7a17b
use patches again in scatter plots
Jaroslav Hajek <highegg@gmail.com>
parents:
10082
diff
changeset
|
253 "faces", 1, "vertices", vert, |
614ad9e7a17b
use patches again in scatter plots
Jaroslav Hajek <highegg@gmail.com>
parents:
10082
diff
changeset
|
254 "facecolor", "none", "edgecolor", "flat", |
614ad9e7a17b
use patches again in scatter plots
Jaroslav Hajek <highegg@gmail.com>
parents:
10082
diff
changeset
|
255 "cdata", c, |
614ad9e7a17b
use patches again in scatter plots
Jaroslav Hajek <highegg@gmail.com>
parents:
10082
diff
changeset
|
256 "marker", marker, "markersize", s, |
614ad9e7a17b
use patches again in scatter plots
Jaroslav Hajek <highegg@gmail.com>
parents:
10082
diff
changeset
|
257 "linestyle", "none"); |
614ad9e7a17b
use patches again in scatter plots
Jaroslav Hajek <highegg@gmail.com>
parents:
10082
diff
changeset
|
258 if (filled) |
614ad9e7a17b
use patches again in scatter plots
Jaroslav Hajek <highegg@gmail.com>
parents:
10082
diff
changeset
|
259 set(h, "markerfacecolor", "flat"); |
614ad9e7a17b
use patches again in scatter plots
Jaroslav Hajek <highegg@gmail.com>
parents:
10082
diff
changeset
|
260 endif |
10078 | 261 endif |
262 else | |
263 ## FIXME: round the size to one decimal place. It's not quite right, though. | |
264 [ss, idx] = unique_idx (ceil (s*10) / 10); | |
265 for i = 1:rows (ss) | |
266 h = render_size_color (hg, vert(idx{i},:), ss(i), c, marker, filled); | |
267 endfor | |
268 endif | |
269 endfunction | |
270 | |
8257 | 271 function update_props (h, d) |
272 lw = get (h, "linewidth"); | |
273 m = get (h, "marker"); | |
274 fc = get (h, "markerfacecolor"); | |
275 ec = get (h, "markeredgecolor"); | |
276 kids = get (h, "children"); | |
277 | |
278 for i = 1 : numel (kids) | |
279 set (kids (i), "linewidth", lw, "marker", m, "markerfacecolor", fc, | |
280 "edgecolor", ec) | |
281 endfor | |
282 endfunction | |
283 | |
284 function update_data (h, d) | |
285 x1 = get (h, "xdata"); | |
286 y1 = get (h, "ydata"); | |
287 z1 = get (h, "zdata"); | |
288 c1 = get (h, "cdata"); | |
289 if (!ischar (c1) && rows (c1) == 1) | |
290 c1 = repmat (c1, numel (x1), 1); | |
291 endif | |
292 size1 = get (h, "sizedata"); | |
293 if (numel (size1) == 1) | |
294 size1 = repmat (size1, numel (x1), 1); | |
295 endif | |
296 hlist = get (h, "children"); | |
297 if (ischar (c1)) | |
9085
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
298 if (isempty (z1)) |
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
299 for i = 1 : length (hlist) |
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
300 set (hlist(i), "vertices", [x1(i), y1(i)], "cdata", c1, |
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
301 "markersize", size1(i)); |
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
302 endfor |
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
303 else |
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
304 for i = 1 : length (hlist) |
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
305 set (hlist(i), "vertices", [x1(i), y1(i), z1(i)], "cdata", c1, |
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
306 "markersize", size1(i)); |
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
307 endfor |
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
308 endif |
8257 | 309 else |
9085
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
310 if (isempty (z1)) |
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
311 for i = 1 : length (hlist) |
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
312 set (hlist(i), "vertices", [x1(i), y1(i)], "cdata", |
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
313 reshape(c1(i,:),[1, size(c1)(2:end)]), "markersize", size1(i)); |
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
314 endfor |
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
315 else |
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
316 for i = 1 : length (hlist) |
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
317 set (hlist(i), "vertices", [x1(i), y1(i), z1(i)], "cdata", |
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
318 reshape(c1(i,:),[1, size(c1)(2:end)]), "markersize", size1(i)); |
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
319 endfor |
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
320 endif |
8257 | 321 endif |
322 endfunction |