comparison scripts/plot/util/subplot.m @ 17860:93edd282a2bc

subplot.m: Matlab compatibility when "position" given (bug #40480). * scripts/plot/util/subplot.m: Replicate subplot code for the case where "position" arg given. When "position" given, use it for all calculations rather than "outerposition" as in normal code.
author Rik <rik@octave.org>
date Wed, 06 Nov 2013 11:14:40 -0800
parents d63878346099
children 446c46af4b42
comparison
equal deleted inserted replaced
17859:5e4cac8f4e43 17860:93edd282a2bc
190 set (0, "defaultaxesunits", "normalized"); 190 set (0, "defaultaxesunits", "normalized");
191 set (cf, "units", "pixels"); 191 set (cf, "units", "pixels");
192 192
193 ## FIXME: At the moment we force gnuplot to use the aligned mode 193 ## FIXME: At the moment we force gnuplot to use the aligned mode
194 ## which will set "activepositionproperty" to "position". 194 ## which will set "activepositionproperty" to "position".
195 ## Τhis can yield to text overlap between labels and titles 195 ## This can yield to text overlap between labels and titles.
196 ## see bug #31610 196 ## See bug #31610.
197 if (strcmp (get (cf, "__graphics_toolkit__"), "gnuplot")) 197 if (strcmp (get (cf, "__graphics_toolkit__"), "gnuplot"))
198 align_axes = true; 198 align_axes = true;
199 endif 199 endif
200 200
201 ## Oh, the things we do for Matlab compatibility. Using the "position"
202 ## argument changes things so much that it became clearer to replicate
203 ## large chunks of code rather than have lots of if/else statements.
201 if (! have_position) 204 if (! have_position)
205 ## Normal case where subplot indices have been given
202 pos = subplot_position (rows, cols, index, "position"); 206 pos = subplot_position (rows, cols, index, "position");
203 outerpos = subplot_position (rows, cols, index, "outerposition"); 207 outerpos = subplot_position (rows, cols, index, "outerposition");
204 box = [pos(1:2), pos(1:2)+pos(3:4)]; 208 box = [pos(1:2), pos(1:2)+pos(3:4)];
205 outerbox = [outerpos(1:2), outerpos(1:2)+outerpos(3:4)]; 209 outerbox = [outerpos(1:2), outerpos(1:2)+outerpos(3:4)];
206 looseinset = [box(1:2)-outerbox(1:2), outerbox(3:4)-box(3:4)]; 210 looseinset = [box(1:2)-outerbox(1:2), outerbox(3:4)-box(3:4)];
211
207 if (align_axes) 212 if (align_axes)
208 activepositionproperty = "position"; 213 activepositionproperty = "position";
209 else 214 else
210 activepositionproperty = "outerposition"; 215 activepositionproperty = "outerposition";
211 endif 216 endif
212 endif 217
213 218 set (cf, "nextplot", "add");
214 set (cf, "nextplot", "add"); 219
215 220 found = false;
216 found = false; 221 kids = get (cf, "children");
217 kids = get (cf, "children"); 222 for child = kids(:)'
218 for child = kids(:)' 223 ## Check whether this child is still valid; this might not be the
219 ## Check whether this child is still valid; this might not be the 224 ## case anymore due to the deletion of previous children (due to
220 ## case anymore due to the deletion of previous children (due to 225 ## "deletefcn" callback or for legends/colorbars that are deleted
221 ## "deletefcn" callback or for legends/colorbars that are deleted 226 ## with their corresponding axes).
222 ## with their corresponding axes). 227 if (! ishandle (child))
223 if (! ishandle (child)) 228 continue;
224 continue; 229 endif
230 if (strcmp (get (child, "type"), "axes"))
231 ## Skip legend and colorbar objects.
232 if (any (strcmp (get (child, "tag"), {"legend", "colorbar"})))
233 continue;
234 endif
235 objpos = get (child, "outerposition");
236 if (all (abs (objpos - outerpos) < eps) && ! replace_axes)
237 ## If the new axes are in exactly the same position
238 ## as an existing axes object, use the existing axes.
239 found = true;
240 hsubplot = child;
241 else
242 ## If the new axes overlap an old axes object, delete the old axes.
243 if (align_axes)
244 objpos = get (child, "position");
245 endif
246 x0 = pos(1);
247 x1 = x0 + pos(3);
248 y0 = pos(2);
249 y1 = y0 + pos(4);
250 objx0 = objpos(1);
251 objx1 = objx0 + objpos(3);
252 objy0 = objpos(2);
253 objy1 = objy0 + objpos(4);
254 if (! (x0 >= objx1 || x1 <= objx0 || y0 >= objy1 || y1 <= objy0))
255 delete (child);
256 endif
257 endif
258 endif
259 endfor
260
261 if (found)
262 ## Switch to existing subplot
263 set (cf, "currentaxes", hsubplot);
264 else
265 hsubplot = axes ("box", "off",
266 "position", pos,
267 "looseinset", looseinset,
268 "activepositionproperty", activepositionproperty,
269 varargin{:});
270 addproperty ("subplot_align", hsubplot, "boolean", true);
271 addlistener (hsubplot, "position", @subplot_align);
272 if (! align_axes)
273 set (hsubplot, "subplot_align", false)
274 subplot_align (hsubplot)
275 endif
225 endif 276 endif
226 if (strcmp (get (child, "type"), "axes")) 277 else
227 ## Skip legend and colorbar objects. 278 ## "position" attribute given
228 if (any (strcmp (get (child, "tag"), {"legend", "colorbar"}))) 279 if (align_axes)
280 activepositionproperty = "position";
281 else
282 activepositionproperty = "outerposition";
283 endif
284
285 set (cf, "nextplot", "add");
286
287 found = false;
288 kids = get (cf, "children");
289 for child = kids(:)'
290 ## Check whether this child is still valid; this might not be the
291 ## case anymore due to the deletion of previous children (due to
292 ## "deletefcn" callback or for legends/colorbars that are deleted
293 ## with their corresponding axes).
294 if (! ishandle (child))
229 continue; 295 continue;
230 endif 296 endif
231 objpos = get (child, "outerposition"); 297 if (strcmp (get (child, "type"), "axes"))
232 if (all (abs (objpos - outerpos) < eps) && ! replace_axes) 298 ## Skip legend and colorbar objects.
233 ## If the new axes are in exactly the same position 299 if (any (strcmp (get (child, "tag"), {"legend", "colorbar"})))
234 ## as an existing axes object, use the existing axes. 300 continue;
235 found = true;
236 hsubplot = child;
237 else
238 ## If the new axes overlap an old axes object, delete the old axes.
239 if (align_axes)
240 objpos = get (child, "position");
241 endif 301 endif
242 x0 = pos(1); 302 objpos = get (child, "position");
243 x1 = x0 + pos(3); 303 if (all (abs (objpos - pos) < eps) && ! replace_axes)
244 y0 = pos(2); 304 ## If the new axes are in exactly the same position
245 y1 = y0 + pos(4); 305 ## as an existing axes object, use the existing axes.
246 objx0 = objpos(1); 306 found = true;
247 objx1 = objx0 + objpos(3); 307 hsubplot = child;
248 objy0 = objpos(2); 308 else
249 objy1 = objy0 + objpos(4); 309 ## If the new axes overlap an old axes object, delete the old axes.
250 if (! (x0 >= objx1 || x1 <= objx0 || y0 >= objy1 || y1 <= objy0)) 310 x0 = pos(1);
251 delete (child); 311 x1 = x0 + pos(3);
312 y0 = pos(2);
313 y1 = y0 + pos(4);
314 objx0 = objpos(1);
315 objx1 = objx0 + objpos(3);
316 objy0 = objpos(2);
317 objy1 = objy0 + objpos(4);
318 if (! (x0 >= objx1 || x1 <= objx0 || y0 >= objy1 || y1 <= objy0))
319 delete (child);
320 endif
252 endif 321 endif
253 endif 322 endif
323 endfor
324
325 if (found)
326 ## Switch to existing subplot
327 set (cf, "currentaxes", hsubplot);
328 else
329 hsubplot = axes ("box", "off",
330 "position", pos,
331 "activepositionproperty", activepositionproperty,
332 varargin{:});
333 addproperty ("subplot_align", hsubplot, "boolean", true);
334 addlistener (hsubplot, "position", @subplot_align);
335 if (! align_axes)
336 set (hsubplot, "subplot_align", false)
337 subplot_align (hsubplot)
338 endif
254 endif 339 endif
255 endfor 340 endif # ! have_position
256
257 if (found)
258 ## Switch to existing subplot
259 set (cf, "currentaxes", hsubplot);
260 else
261 hsubplot = axes ("box", "off",
262 "position", pos,
263 "looseinset", looseinset,
264 "activepositionproperty", activepositionproperty,
265 varargin{:});
266 addproperty ("subplot_align", hsubplot, "boolean", true);
267 addlistener (hsubplot, "position", @subplot_align);
268 if (! align_axes)
269 set (hsubplot, "subplot_align", false)
270 subplot_align (hsubplot)
271 endif
272 endif
273 341
274 unwind_protect_cleanup 342 unwind_protect_cleanup
275 set (0, "defaultaxesunits", axesunits); 343 set (0, "defaultaxesunits", axesunits);
276 set (cf, "units", figureunits); 344 set (cf, "units", figureunits);
277 end_unwind_protect 345 end_unwind_protect