changeset 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 350cad34b0f8
children 64d603f8bcd9
files scripts/plot/subplot.m
diffstat 1 files changed, 91 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/plot/subplot.m	Thu Jul 18 22:16:47 2013 -0400
+++ b/scripts/plot/subplot.m	Thu Jul 18 22:17:35 2013 -0400
@@ -174,12 +174,15 @@
     endif
 
     if (! have_position)
+      pos = subplot_position (rows, cols, index, "position");
+      outerpos = subplot_position (rows, cols, index, "outerposition");
+      box = [pos(1:2), pos(1:2)+pos(3:4)];
+      outerbox = [outerpos(1:2), outerpos(1:2)+outerpos(3:4)];
+      looseinset = [box(1:2)-outerbox(1:2), outerbox(3:4)-box(3:4)];
       if (align_axes)
-        pos = subplot_position (rows, cols, index, "position");
-      elseif (strcmp (get (cf, "__graphics_toolkit__"), "gnuplot"))
-        pos = subplot_position (rows, cols, index, "outerpositiontight");
+        activepositionproperty = "position";
       else
-        pos = subplot_position (rows, cols, index, "outerposition");
+        activepositionproperty = "outerposition";
       endif
     endif
 
@@ -210,7 +213,7 @@
           ## If the new axes are in exactly the same position as an
           ## existing axes object, use the existing axes.
           found = true;
-          tmp = child;
+          hsubplot = child;
         else
           ## If the new axes overlap an old axes object, delete the old
           ## axes.
@@ -231,13 +234,18 @@
 
     if (found)
       set (cf, "currentaxes", tmp);
-    elseif (align_axes)
-      tmp = axes ("box", "off", "position", pos, varargin{:});
-    elseif (strcmp (get (cf, "__graphics_toolkit__"), "gnuplot"))
-      tmp = axes ("box", "off", "outerposition", pos, varargin{:});
     else
-      tmp = axes ("looseinset", [0 0 0 0], "box", "off", "outerposition", pos,
-                  "autopos_tag", "subplot", varargin{:});
+      hsubplot = axes ("box", "off",
+                       "position", pos,
+                       "looseinset", looseinset,
+                       "activepositionproperty", activepositionproperty,
+                       varargin{:});
+      addproperty ("subplot_align", hsubplot, "boolean", true);
+      addlistener (hsubplot, "position", @subplot_align);
+      if (! align_axes)
+        set (hsubplot, "subplot_align", false)
+        subplot_align (hsubplot)
+      endif
     endif
 
   unwind_protect_cleanup
@@ -246,7 +254,7 @@
   end_unwind_protect
 
   if (nargout > 0)
-    h = tmp;
+    h = hsubplot;
   endif
 
 endfunction
@@ -327,6 +335,30 @@
 
 endfunction
 
+function subplot_align (h, varargin)
+  persistent updating = false
+  if (! updating)
+    unwind_protect
+      updating = true;
+      hfig = ancestor (h, "figure");
+      hsubplots = findall (hfig, 'type', 'axes', 'subplot_align', 'off');
+      if (! isempty (hsubplots))
+        tightinset = get (hsubplots, 'tightinset');
+        if (iscell (tightinset))
+          tightinset = max (cell2mat (tightinset));
+        endif
+        looseinset = get (hsubplots, 'looseinset');
+        if (iscell (looseinset))
+          looseinset = max (cell2mat (looseinset));
+        endif
+        looseinset = max (tightinset, looseinset);
+        set (hsubplots, 'looseinset', looseinset);
+      endif
+    unwind_protect_cleanup
+      updating = false;
+    end_unwind_protect
+  endif
+endfunction
 
 %!demo
 %! clf;
@@ -365,22 +397,61 @@
 %! title ('title (1,2,1)');
 
 %!demo
+%! clf;
 %! x = 0:10;
-%! subplot (221);
+%! ax(1) = subplot (221);
+%! set (ax(1), 'tag', '1');
+%! plot (x, rand (3, 11))
+%! title ('x & y labels & ticklabels');
+%! xlabel xlabel
+%! ylabel ylabel
+%! ax(2) = subplot (222);
+%! set (ax(2), 'tag', '2');
+%! plot (x, rand (3, 11))
+%! title ('no labels');
+%! axis ('nolabel','tic')
+%! ax(3) = subplot (223);
+%! set (ax(3), 'tag', '3');
+%! plot (x, rand (3, 11))
+%! title ('no labels');
+%! axis ('nolabel','tic')
+%! ax(4) = subplot (224);
+%! set (ax(4), 'tag', '4');
 %! plot (x, rand (3, 11))
 %! title ('x & y labels & ticklabels');
 %! xlabel xlabel
 %! ylabel ylabel
+
+%!demo
+%! x = 0:10;
+%! subplot (221);
+%! plot (x, rand (3, 11))
+%! ylim ([0, 1]);
+%! text (0.5, 0.5, '{x,y}labels & {x,y}ticklabels', ...
+%!       'horizontalalignment', 'center', ...
+%!       'units', 'normalized');
+%! xlabel xlabel
+%! ylabel ylabel
+%! title title
 %! subplot (222);
 %! plot (x, rand (3, 11))
-%! title ('no labels');
-%! axis ('nolabel','tic');
+%! axis ('labely');
+%! ylabel ylabel
+%! text (0.5, 0.5, 'no xlabels, xticklabels', ...
+%!       'horizontalalignment', 'center', ...
+%!       'units', 'normalized');
 %! subplot (223);
 %! plot (x, rand (3, 11))
-%! title ('no labels');
-%! axis ('nolabel','tic');
+%! axis ('labelx');
+%! text (0.5, 0.5, 'no ylabels, yticklabels', ...
+%!       'horizontalalignment', 'center', ...
+%!       'units', 'normalized');
+%! xlabel xlabel
+%! title title
 %! subplot (224);
 %! plot (x, rand (3, 11))
-%! title ('x & y labels & ticklabels');
-%! xlabel xlabel
-%! ylabel ylabel
+%! axis ('nolabel','tic');
+%! text (0.5, 0.5, 'no {x,y}labels, {x,y}ticklabels', ...
+%!       'horizontalalignment', 'center', ...
+%!       'units', 'normalized');
+