comparison scripts/plot/subplot.m @ 11305:c9df571efe95

subplot.m: Add suppport for "align" and "replace" options.
author Ben Abbott <bpabbott@mac.com>
date Thu, 02 Dec 2010 18:46:10 -0500
parents fe3c3dfc07eb
children c776f063fefe
comparison
equal deleted inserted replaced
11304:c9fefa096ce2 11305:c9df571efe95
58 ## @end deftypefn 58 ## @end deftypefn
59 59
60 ## Author: Vinayak Dutt <Dutt.Vinayak@mayo.EDU> 60 ## Author: Vinayak Dutt <Dutt.Vinayak@mayo.EDU>
61 ## Adapted-By: jwe 61 ## Adapted-By: jwe
62 62
63 function h = subplot (rows, columns, index) 63 function h = subplot (rows, columns, index, varargin)
64 64
65 if (nargin != 3 && nargin != 1) 65 align_axes = false;
66 replace_axes = false;
67
68 if (! (nargin >= 3) && nargin != 1)
66 print_usage (); 69 print_usage ();
70 elseif (nargin > 3)
71 for n = 1:numel(varargin)
72 switch lower(varargin{n})
73 case "align"
74 align_axes = true;
75 case "replace"
76 replace_axes = true;
77 otherwise
78 print_usage ();
79 endswitch
80 endfor
67 endif 81 endif
68 82
69 if (nargin == 1) 83 if (nargin == 1)
70 84
71 if (! (isscalar (rows) && rows >= 0)) 85 if (! (isscalar (rows) && rows >= 0))
121 if (strcmp (get (child, "tag"), "legend") 135 if (strcmp (get (child, "tag"), "legend")
122 || strcmp (get (child, "tag"), "colorbar")) 136 || strcmp (get (child, "tag"), "colorbar"))
123 continue; 137 continue;
124 endif 138 endif
125 objpos = get (child, "position"); 139 objpos = get (child, "position");
126 if (all (objpos == pos)) 140 if (all (objpos == pos) && ! replace_axes)
127 ## If the new axes are in exactly the same position as an 141 ## If the new axes are in exactly the same position as an
128 ## existing axes object, use the existing axes. 142 ## existing axes object, use the existing axes.
129 found = true; 143 found = true;
130 tmp = child; 144 tmp = child;
131 else 145 else
149 if (found) 163 if (found)
150 set (cf, "currentaxes", tmp); 164 set (cf, "currentaxes", tmp);
151 else 165 else
152 pos = subplot_position (rows, columns, index, "outerposition", units); 166 pos = subplot_position (rows, columns, index, "outerposition", units);
153 pos2 = subplot_position (rows, columns, index, "position", units); 167 pos2 = subplot_position (rows, columns, index, "position", units);
154 tmp = axes ("outerposition", pos, 168 tmp = axes ("outerposition", pos, "position", pos2,
155 "position", pos2, 169 "activepositionproperty", "outerposition");
156 "activepositionproperty", "position"); 170 endif
171
172 if (align_axes && strcmp (get (cf, "__backend__"), "gnuplot"))
173 set (tmp, "activepositionproperty", "position");
157 endif 174 endif
158 175
159 unwind_protect_cleanup 176 unwind_protect_cleanup
160 set (0, "defaultaxesunits", units); 177 set (0, "defaultaxesunits", units);
161 end_unwind_protect 178 end_unwind_protect
263 %! ylabel (sprintf ("ylabel #%d:%d", 1, 3)) 280 %! ylabel (sprintf ("ylabel #%d:%d", 1, 3))
264 %! title (sprintf ("title #%d:%d", 1, 3)) 281 %! title (sprintf ("title #%d:%d", 1, 3))
265 %! text (0.5, 0.5, sprintf('subplot(%d,%d,%d:%d)', r, c, 1, 3), fmt{:}) 282 %! text (0.5, 0.5, sprintf('subplot(%d,%d,%d:%d)', r, c, 1, 3), fmt{:})
266 %! axis ([0 1 0 1]) 283 %! axis ([0 1 0 1])
267 284
285 %!demo
286 %! clf
287 %! x = 0:1;
288 %! for n = 1:4
289 %! subplot (2, 2, n, "align")
290 %! plot (x, x)
291 %! xlabel (sprintf ("xlabel (2,2,%d)", n))
292 %! ylabel (sprintf ("ylabel (2,2,%d)", n))
293 %! title (sprintf ("title (2,2,%d)", n))
294 %! endfor
295 %! subplot (1, 2, 1, "align")
296 %! plot (x, x)
297 %! xlabel (sprintf ("xlabel (1,2,%d)", n))
298 %! ylabel (sprintf ("ylabel (1,2,%d)", n))
299 %! title (sprintf ("title (1,2,%d)", n))
300