Mercurial > hg > octave-lyh
annotate scripts/plot/private/__scatter__.m @ 10078:2f435ed48143
optimize scatter plots
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Sun, 10 Jan 2010 21:34:30 +0100 |
parents | 1aeb39118764 |
children | 28af9aa6f9ed |
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 | |
10078 | 123 if (numel (x) <= 20) |
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) | |
133 h = __go_line__ (hg, "xdata", x(i), "ydata", y(i), "zdata", z(i,:), | |
134 "color", c, "marker", marker, | |
135 "markersize", s(i), "linestyle", "none"); | |
136 if (filled) | |
137 set(h, "markerfacecolor", c); | |
138 endif | |
139 endfor | |
140 else | |
141 if (rows (c) == 1) | |
142 c = repmat (c, numel (x), 1); | |
8257 | 143 endif |
10078 | 144 |
145 for i = 1 : numel (x) | |
146 h = __go_line__ (hg, "xdata", x(i), "ydata", y(i), "zdata", z(i,:), | |
147 "color", c(i,:), "marker", marker, | |
148 "markersize", s(i), "linestyle", "none"); | |
149 if (filled) | |
150 set(h, "markerfacecolor", c(i,:)); | |
151 endif | |
152 endfor | |
153 endif | |
154 | |
8257 | 155 else |
10078 | 156 |
157 ## For larger numbers of points, we split the points by common color. | |
158 | |
159 vert = [x, y, z]; | |
160 | |
161 if (ischar (c) || rows (c) == 1) | |
162 h = render_size_color (hg, vert, s, c, marker, filled); | |
163 else | |
164 [cc, idx] = unique_idx (c, "rows"); | |
165 if (isscalar (s)) | |
166 for i = 1:rows (x) | |
167 h = render_size_color (hg, vert(idx{i},:), s, cc(i,:), marker, filled); | |
168 endfor | |
169 else | |
170 for i = 1:rows (x) | |
171 h = render_size_color (hg, vert(idx{i},:), s(idx{i}), cc(i,:), marker, filled); | |
172 endfor | |
8257 | 173 endif |
7189 | 174 endif |
10078 | 175 |
176 endif | |
177 | |
178 if (! ischar (c)) | |
179 ax = get (hg, "parent"); | |
180 clim = get (ax, "clim"); | |
181 if (min(c(:)) < clim(1)) | |
182 clim(1) = min(c(:)); | |
183 set (ax, "clim", clim); | |
184 endif | |
185 if (max(c(:)) > clim(2)) | |
186 set (ax, "clim", [clim(1), max(c(:))]); | |
187 endif | |
7189 | 188 endif |
189 | |
8257 | 190 addproperty ("linewidth", hg, "patchlinewidth", 0.5); |
191 addproperty ("marker", hg, "patchmarker", marker); | |
192 if (numel (x) > 0) | |
193 addproperty ("markerfacecolor", hg, "patchmarkerfacecolor", "none"); | |
194 addproperty ("markeredgecolor", hg, "patchmarkeredgecolor", "none"); | |
195 else | |
196 addproperty ("markerfacecolor", hg, "patchmarkerfacecolor", | |
197 get (h, "markerfacecolor")); | |
198 addproperty ("markeredgecolor", hg, "patchmarkeredgecolor", | |
10078 | 199 get (h, "color")); |
8257 | 200 endif |
201 addlistener (hg, "linewidth", @update_props); | |
202 addlistener (hg, "marker", @update_props); | |
203 addlistener (hg, "markerfacecolor", @update_props); | |
204 addlistener (hg, "markeredgecolor", @update_props); | |
205 | |
206 if (! isempty (newargs)) | |
207 set (hg, newargs{:}) | |
208 endif | |
209 | |
7189 | 210 endfunction |
8257 | 211 |
10078 | 212 function [y, idx] = unique_idx (x, byrows) |
213 if (nargin == 2) | |
214 [xx, idx] = sortrows (x); | |
215 n = rows (x); | |
216 jdx = find (any (xx(1:n-1,:) != xx(2:n,:), 2)); | |
217 jdx(n) = n; | |
218 y = xx(jdx,:); | |
219 else | |
220 [xx, idx] = sort (x); | |
221 n = length (x); | |
222 jdx = find (xx(1:n-1,:) != xx(2:n,:)); | |
223 jdx(n) = n; | |
224 y = xx(jdx); | |
225 endif | |
226 | |
227 if (nargin == 2 || columns (x) == 1) | |
228 idx = mat2cell (idx, diff ([0; jdx]), 1); | |
229 else | |
230 idx = mat2cell (idx, 1, diff ([0, jdx])); | |
231 endif | |
232 endfunction | |
233 | |
234 function h = render_size_color(hg, vert, s, c, marker, filled) | |
235 if (isscalar (s)) | |
236 x = vert(:,1); | |
237 y = vert(:,2); | |
238 z = vert(:,3:end); | |
239 h = __go_line__ (hg, "xdata", x, "ydata", y, "zdata", z, | |
240 "color", c, "marker", marker, | |
241 "markersize", s, "linestyle", "none"); | |
242 if (filled) | |
243 set(h, "markerfacecolor", c); | |
244 endif | |
245 else | |
246 ## FIXME: round the size to one decimal place. It's not quite right, though. | |
247 [ss, idx] = unique_idx (ceil (s*10) / 10); | |
248 for i = 1:rows (ss) | |
249 h = render_size_color (hg, vert(idx{i},:), ss(i), c, marker, filled); | |
250 endfor | |
251 endif | |
252 endfunction | |
253 | |
8257 | 254 function update_props (h, d) |
255 lw = get (h, "linewidth"); | |
256 m = get (h, "marker"); | |
257 fc = get (h, "markerfacecolor"); | |
258 ec = get (h, "markeredgecolor"); | |
259 kids = get (h, "children"); | |
260 | |
261 for i = 1 : numel (kids) | |
262 set (kids (i), "linewidth", lw, "marker", m, "markerfacecolor", fc, | |
263 "edgecolor", ec) | |
264 endfor | |
265 endfunction | |
266 | |
267 function update_data (h, d) | |
268 x1 = get (h, "xdata"); | |
269 y1 = get (h, "ydata"); | |
270 z1 = get (h, "zdata"); | |
271 c1 = get (h, "cdata"); | |
272 if (!ischar (c1) && rows (c1) == 1) | |
273 c1 = repmat (c1, numel (x1), 1); | |
274 endif | |
275 size1 = get (h, "sizedata"); | |
276 if (numel (size1) == 1) | |
277 size1 = repmat (size1, numel (x1), 1); | |
278 endif | |
279 hlist = get (h, "children"); | |
280 if (ischar (c1)) | |
9085
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
281 if (isempty (z1)) |
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
282 for i = 1 : length (hlist) |
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
283 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
|
284 "markersize", size1(i)); |
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
285 endfor |
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
286 else |
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
287 for i = 1 : length (hlist) |
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
288 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
|
289 "markersize", size1(i)); |
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
290 endfor |
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
291 endif |
8257 | 292 else |
9085
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
293 if (isempty (z1)) |
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
294 for i = 1 : length (hlist) |
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
295 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
|
296 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
|
297 endfor |
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
298 else |
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), z1(i)], "cdata", |
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
301 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
|
302 endfor |
136e72b9afa8
correct indexing of cdata for scatter
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
303 endif |
8257 | 304 endif |
305 endfunction |