comparison scripts/plot/subplot.m @ 17012:942d892524b3

Handle alignment of subplots in subplot.m. script/plot/subplot.m: Introduce listener (subplot_align) to manage alignment of subplots by synchronizing looseinset and tightinset across all subplots. "activepositionproperty". This obsoletes using autopos_tag property. Modify demo and add new demo.
author Ben Abbott <bpabbott@mac.com>
date Thu, 18 Jul 2013 22:17:35 -0400
parents 47cc8e3d9183
children ddc62f0c7a35
comparison
equal deleted inserted replaced
17011:350cad34b0f8 17012:942d892524b3
172 if (strcmp (get (cf, "__graphics_toolkit__"), "gnuplot")) 172 if (strcmp (get (cf, "__graphics_toolkit__"), "gnuplot"))
173 align_axes = true; 173 align_axes = true;
174 endif 174 endif
175 175
176 if (! have_position) 176 if (! have_position)
177 pos = subplot_position (rows, cols, index, "position");
178 outerpos = subplot_position (rows, cols, index, "outerposition");
179 box = [pos(1:2), pos(1:2)+pos(3:4)];
180 outerbox = [outerpos(1:2), outerpos(1:2)+outerpos(3:4)];
181 looseinset = [box(1:2)-outerbox(1:2), outerbox(3:4)-box(3:4)];
177 if (align_axes) 182 if (align_axes)
178 pos = subplot_position (rows, cols, index, "position"); 183 activepositionproperty = "position";
179 elseif (strcmp (get (cf, "__graphics_toolkit__"), "gnuplot"))
180 pos = subplot_position (rows, cols, index, "outerpositiontight");
181 else 184 else
182 pos = subplot_position (rows, cols, index, "outerposition"); 185 activepositionproperty = "outerposition";
183 endif 186 endif
184 endif 187 endif
185 188
186 set (cf, "nextplot", "add"); 189 set (cf, "nextplot", "add");
187 190
208 endif 211 endif
209 if (all (abs (objpos - pos) < eps) && ! replace_axes) 212 if (all (abs (objpos - pos) < eps) && ! replace_axes)
210 ## If the new axes are in exactly the same position as an 213 ## If the new axes are in exactly the same position as an
211 ## existing axes object, use the existing axes. 214 ## existing axes object, use the existing axes.
212 found = true; 215 found = true;
213 tmp = child; 216 hsubplot = child;
214 else 217 else
215 ## If the new axes overlap an old axes object, delete the old 218 ## If the new axes overlap an old axes object, delete the old
216 ## axes. 219 ## axes.
217 x0 = pos(1); 220 x0 = pos(1);
218 x1 = x0 + pos(3); 221 x1 = x0 + pos(3);
229 endif 232 endif
230 endfor 233 endfor
231 234
232 if (found) 235 if (found)
233 set (cf, "currentaxes", tmp); 236 set (cf, "currentaxes", tmp);
234 elseif (align_axes)
235 tmp = axes ("box", "off", "position", pos, varargin{:});
236 elseif (strcmp (get (cf, "__graphics_toolkit__"), "gnuplot"))
237 tmp = axes ("box", "off", "outerposition", pos, varargin{:});
238 else 237 else
239 tmp = axes ("looseinset", [0 0 0 0], "box", "off", "outerposition", pos, 238 hsubplot = axes ("box", "off",
240 "autopos_tag", "subplot", varargin{:}); 239 "position", pos,
240 "looseinset", looseinset,
241 "activepositionproperty", activepositionproperty,
242 varargin{:});
243 addproperty ("subplot_align", hsubplot, "boolean", true);
244 addlistener (hsubplot, "position", @subplot_align);
245 if (! align_axes)
246 set (hsubplot, "subplot_align", false)
247 subplot_align (hsubplot)
248 endif
241 endif 249 endif
242 250
243 unwind_protect_cleanup 251 unwind_protect_cleanup
244 set (0, "defaultaxesunits", axesunits); 252 set (0, "defaultaxesunits", axesunits);
245 set (cf, "units", figureunits); 253 set (cf, "units", figureunits);
246 end_unwind_protect 254 end_unwind_protect
247 255
248 if (nargout > 0) 256 if (nargout > 0)
249 h = tmp; 257 h = hsubplot;
250 endif 258 endif
251 259
252 endfunction 260 endfunction
253 261
254 function pos = subplot_position (rows, cols, index, position_property) 262 function pos = subplot_position (rows, cols, index, position_property)
325 pos = [x0, y0, width, height]; 333 pos = [x0, y0, width, height];
326 endif 334 endif
327 335
328 endfunction 336 endfunction
329 337
338 function subplot_align (h, varargin)
339 persistent updating = false
340 if (! updating)
341 unwind_protect
342 updating = true;
343 hfig = ancestor (h, "figure");
344 hsubplots = findall (hfig, 'type', 'axes', 'subplot_align', 'off');
345 if (! isempty (hsubplots))
346 tightinset = get (hsubplots, 'tightinset');
347 if (iscell (tightinset))
348 tightinset = max (cell2mat (tightinset));
349 endif
350 looseinset = get (hsubplots, 'looseinset');
351 if (iscell (looseinset))
352 looseinset = max (cell2mat (looseinset));
353 endif
354 looseinset = max (tightinset, looseinset);
355 set (hsubplots, 'looseinset', looseinset);
356 endif
357 unwind_protect_cleanup
358 updating = false;
359 end_unwind_protect
360 endif
361 endfunction
330 362
331 %!demo 363 %!demo
332 %! clf; 364 %! clf;
333 %! r = 3; 365 %! r = 3;
334 %! c = 3; 366 %! c = 3;
363 %! xlabel ('xlabel (1,2,1)'); 395 %! xlabel ('xlabel (1,2,1)');
364 %! ylabel ('ylabel (1,2,1)'); 396 %! ylabel ('ylabel (1,2,1)');
365 %! title ('title (1,2,1)'); 397 %! title ('title (1,2,1)');
366 398
367 %!demo 399 %!demo
400 %! clf;
368 %! x = 0:10; 401 %! x = 0:10;
369 %! subplot (221); 402 %! ax(1) = subplot (221);
403 %! set (ax(1), 'tag', '1');
370 %! plot (x, rand (3, 11)) 404 %! plot (x, rand (3, 11))
371 %! title ('x & y labels & ticklabels'); 405 %! title ('x & y labels & ticklabels');
372 %! xlabel xlabel 406 %! xlabel xlabel
373 %! ylabel ylabel 407 %! ylabel ylabel
374 %! subplot (222); 408 %! ax(2) = subplot (222);
409 %! set (ax(2), 'tag', '2');
375 %! plot (x, rand (3, 11)) 410 %! plot (x, rand (3, 11))
376 %! title ('no labels'); 411 %! title ('no labels');
377 %! axis ('nolabel','tic'); 412 %! axis ('nolabel','tic')
378 %! subplot (223); 413 %! ax(3) = subplot (223);
414 %! set (ax(3), 'tag', '3');
379 %! plot (x, rand (3, 11)) 415 %! plot (x, rand (3, 11))
380 %! title ('no labels'); 416 %! title ('no labels');
381 %! axis ('nolabel','tic'); 417 %! axis ('nolabel','tic')
382 %! subplot (224); 418 %! ax(4) = subplot (224);
419 %! set (ax(4), 'tag', '4');
383 %! plot (x, rand (3, 11)) 420 %! plot (x, rand (3, 11))
384 %! title ('x & y labels & ticklabels'); 421 %! title ('x & y labels & ticklabels');
385 %! xlabel xlabel 422 %! xlabel xlabel
386 %! ylabel ylabel 423 %! ylabel ylabel
424
425 %!demo
426 %! x = 0:10;
427 %! subplot (221);
428 %! plot (x, rand (3, 11))
429 %! ylim ([0, 1]);
430 %! text (0.5, 0.5, '{x,y}labels & {x,y}ticklabels', ...
431 %! 'horizontalalignment', 'center', ...
432 %! 'units', 'normalized');
433 %! xlabel xlabel
434 %! ylabel ylabel
435 %! title title
436 %! subplot (222);
437 %! plot (x, rand (3, 11))
438 %! axis ('labely');
439 %! ylabel ylabel
440 %! text (0.5, 0.5, 'no xlabels, xticklabels', ...
441 %! 'horizontalalignment', 'center', ...
442 %! 'units', 'normalized');
443 %! subplot (223);
444 %! plot (x, rand (3, 11))
445 %! axis ('labelx');
446 %! text (0.5, 0.5, 'no ylabels, yticklabels', ...
447 %! 'horizontalalignment', 'center', ...
448 %! 'units', 'normalized');
449 %! xlabel xlabel
450 %! title title
451 %! subplot (224);
452 %! plot (x, rand (3, 11))
453 %! axis ('nolabel','tic');
454 %! text (0.5, 0.5, 'no {x,y}labels, {x,y}ticklabels', ...
455 %! 'horizontalalignment', 'center', ...
456 %! 'units', 'normalized');
457