comparison scripts/plot/colorbar.m @ 10558:23c1910dbd8e

Fix colorbar() bugs for manual plotboxaspectratio.
author Ben Abbott <bpabbott@mac.com>
date Fri, 23 Apr 2010 20:52:10 -0400
parents 95c3e38098bf
children be55736a0783
comparison
equal deleted inserted replaced
10557:e86651d2fde5 10558:23c1910dbd8e
104 cext = get (ax, "clim"); 104 cext = get (ax, "clim");
105 cdiff = (cext(2) - cext(1)) / clen / 2; 105 cdiff = (cext(2) - cext(1)) / clen / 2;
106 cmin = cext(1) + cdiff; 106 cmin = cext(1) + cdiff;
107 cmax = cext(2) - cdiff; 107 cmax = cext(2) - cdiff;
108 108
109 orig_pos = obj.position;
110 orig_opos = obj.outerposition;
111 [pos, cpos, vertical, mirror] = ... 109 [pos, cpos, vertical, mirror] = ...
112 __position_colorbox__ (loc, obj, ancestor (ax, "figure")); 110 __position_colorbox__ (loc, obj, ancestor (ax, "figure"));
113 set (ax, "activepositionproperty", "position", "position", pos); 111 set (ax, "activepositionproperty", "position", "position", pos);
114 112
115 cax = __go_axes__ (get (ax, "parent"), "tag", "colorbar", 113 cax = __go_axes__ (get (ax, "parent"), "tag", "colorbar",
146 endif 144 endif
147 145
148 ctext = text (0, 0, "", "tag", "colorbar","visible", "off", 146 ctext = text (0, 0, "", "tag", "colorbar","visible", "off",
149 "handlevisibility", "off", "xliminclude", "off", 147 "handlevisibility", "off", "xliminclude", "off",
150 "yliminclude", "off", "zliminclude", "off", 148 "yliminclude", "off", "zliminclude", "off",
151 "deletefcn", {@deletecolorbar, cax, orig_pos, orig_opos}); 149 "deletefcn", {@deletecolorbar, cax, obj});
152 150
153 set (cax, "deletefcn", {@resetaxis, orig_pos, orig_opos}); 151 set (cax, "deletefcn", {@resetaxis, obj});
154 152
155 addlistener (ax, "clim", {@update_colorbar_clim, hi, vertical}) 153 addlistener (ax, "clim", {@update_colorbar_clim, hi, vertical})
156 addlistener (ax, "plotboxaspectratio", {@update_colorbar_axis, cax}) 154 addlistener (ax, "plotboxaspectratio", {@update_colorbar_axis, cax, obj})
157 addlistener (ax, "plotboxaspectratiomode", {@update_colorbar_axis, cax}) 155 addlistener (ax, "plotboxaspectratiomode", {@update_colorbar_axis, cax, obj})
158 addlistener (ax, "position", {@update_colorbar_axis, cax}) 156 addlistener (ax, "position", {@update_colorbar_axis, cax, obj})
159 157
160 endif 158 endif
161 159
162 if (nargout > 0) 160 if (nargout > 0)
163 h = cax; 161 h = cax;
164 endif 162 endif
165 endfunction 163 endfunction
166 164
167 function deletecolorbar (h, d, hc, pos, opos) 165 function deletecolorbar (h, d, hc, orig_props)
168 ## Don't delete the colorbar and reset the axis size if the 166 ## Don't delete the colorbar and reset the axis size if the
169 ## parent figure is being deleted. 167 ## parent figure is being deleted.
170 if (ishandle (hc) && strcmp (get (hc, "type"), "axes") && 168 if (ishandle (hc) && strcmp (get (hc, "type"), "axes") &&
171 (isempty (gcbf()) || strcmp (get (gcbf(), "beingdeleted"),"off"))) 169 (isempty (gcbf()) || strcmp (get (gcbf(), "beingdeleted"),"off")))
172 if (strcmp (get (hc, "beingdeleted"), "off")) 170 if (strcmp (get (hc, "beingdeleted"), "off"))
173 delete (hc); 171 delete (hc);
174 endif 172 endif
175 if (!isempty (ancestor (h, "axes")) && 173 if (!isempty (ancestor (h, "axes")) &&
176 strcmp (get (ancestor (h, "axes"), "beingdeleted"), "off")) 174 strcmp (get (ancestor (h, "axes"), "beingdeleted"), "off"))
177 set (ancestor (h, "axes"), "position", pos, "outerposition", opos); 175 set (ancestor (h, "axes"), "position", orig_props.position, ...
176 "outerposition", orig_props.outerposition);
178 endif 177 endif
179 endif 178 endif
180 endfunction 179 endfunction
181 180
182 function resetaxis (h, d, pos, opos) 181 function resetaxis (h, d, orig_props)
183 if (ishandle (h) && strcmp (get (h, "type"), "axes") && 182 if (ishandle (h) && strcmp (get (h, "type"), "axes") &&
184 (isempty (gcbf()) || strcmp (get (gcbf(), "beingdeleted"),"off")) && 183 (isempty (gcbf()) || strcmp (get (gcbf(), "beingdeleted"),"off")) &&
185 ishandle (get (h, "axes"))) 184 ishandle (get (h, "axes")))
186 set (get (h, "axes"), "position", pos, "outerposition", opos); 185 set (get (h, "axes"), "position", orig_props.position, ...
186 "outerposition", orig_props.outerposition);
187 endif 187 endif
188 endfunction 188 endfunction
189 189
190 function update_colorbar_clim (h, d, hi, vert) 190 function update_colorbar_clim (h, d, hi, vert)
191 if (ishandle (h) && strcmp (get (h, "type"), "image") && 191 if (ishandle (h) && strcmp (get (h, "type"), "image") &&
204 set (get (hi, "parent"), "xlim", cext); 204 set (get (hi, "parent"), "xlim", cext);
205 endif 205 endif
206 endif 206 endif
207 endfunction 207 endfunction
208 208
209 function update_colorbar_axis (h, d, cax) 209 function update_colorbar_axis (h, d, cax, orig_props)
210
210 if (ishandle (cax) && strcmp (get (cax, "type"), "axes") && 211 if (ishandle (cax) && strcmp (get (cax, "type"), "axes") &&
211 (isempty (gcbf()) || strcmp (get (gcbf(), "beingdeleted"),"off"))) 212 (isempty (gcbf()) || strcmp (get (gcbf(), "beingdeleted"),"off")))
212 loc = get (cax, "location"); 213 loc = get (cax, "location");
213 obj = get (h); 214 obj = get (h);
215 obj.position = orig_props.position;
216 obj.outerposition = orig_props.outerposition;
214 [pos, cpos, vertical, mirror] = ... 217 [pos, cpos, vertical, mirror] = ...
215 __position_colorbox__ (loc, obj, ancestor (h, "figure")); 218 __position_colorbox__ (loc, obj, ancestor (h, "figure"));
216 219
217 if (vertical) 220 if (vertical)
218 if (mirror) 221 if (mirror)
235 endif 238 endif
236 endfunction 239 endfunction
237 240
238 function [pos, cpos, vertical, mirr] = __position_colorbox__ (cbox, obj, cf) 241 function [pos, cpos, vertical, mirr] = __position_colorbox__ (cbox, obj, cf)
239 242
243 ## This will always represent the position prior to adding the colorbar.
240 pos = obj.position; 244 pos = obj.position;
241 sz = pos(3:4); 245 sz = pos(3:4);
242 246
243 off = 0;
244 if (strcmpi (obj.plotboxaspectratiomode, "manual")) 247 if (strcmpi (obj.plotboxaspectratiomode, "manual"))
245 r = obj.plotboxaspectratio; 248 if (isempty (strfind (cbox, "outside")))
246 if (pos(3) > pos(4)) 249 scale = 1.0;
247 switch (cbox) 250 else
248 case {"east", "eastoutside", "west", "westoutside"} 251 scale = 0.8;
249 off = [(pos(3) - pos(4)) ./ (r(2) / r(1)), 0]; 252 endif
250 endswitch 253 if (isempty (strfind (cbox, "east")) && isempty (strfind (cbox, "west")))
251 else 254 scale = [1, scale];
252 switch (cbox) 255 else
253 case {"north", "northoutside", "south", "southoutside"} 256 scale = [scale, 1];
254 off = [0, (pos(4) - pos(3)) ./ (r(1) / r(2))]; 257 endif
255 ## This shouldn't be here except that gnuplot doesn't have a 258 obj.position = obj.position .* [1, 1, scale];
256 ## square window and so a square aspect ratio is not square. 259 off = 0.5 * (obj.position (3:4) - __actual_axis_position__ (obj)(3:4));
257 ## The corrections are empirical. 260 else
258 if (strcmp (get (cf, "__backend__"), "gnuplot")) 261 off = 0.0;
259 if (length (cbox) > 7 && strcmp (cbox(end-6:end),"outside"))
260 off = off / 2;
261 else
262 off = off / 1.7;
263 endif
264 endif
265 endswitch
266 endif
267 off = off / 2;
268 endif 262 endif
269 263
270 switch (cbox) 264 switch (cbox)
271 case "northoutside" 265 case "northoutside"
272 origin = pos(1:2) + [0., 0.9] .* sz + [1, -1] .* off; 266 origin = pos(1:2) + [0., 0.9] .* sz + [1, -1] .* off;
325 scale = 1.0; 319 scale = 1.0;
326 else 320 else
327 scale = 0.9; 321 scale = 0.9;
328 endif 322 endif
329 if (sz(1) > sz(2)) 323 if (sz(1) > sz(2))
324 ## Ensure north or south colorbars are the proper length
330 dx = (1-scale)*actual_pos(3); 325 dx = (1-scale)*actual_pos(3);
331 cpos(1) = actual_pos(1) + dx/2; 326 cpos(1) = actual_pos(1) + dx/2;
332 cpos(3) = actual_pos(3) - dx; 327 cpos(3) = actual_pos(3) - dx;
333 else 328 else
329 ## Ensure east or west colorbars are the proper height
334 dy = (1-scale)*actual_pos(4); 330 dy = (1-scale)*actual_pos(4);
335 cpos(2) = actual_pos(2) + dy/2; 331 cpos(2) = actual_pos(2) + dy/2;
336 cpos(4) = actual_pos(4) - dy; 332 cpos(4) = actual_pos(4) - dy;
337 endif 333 endif
338 endif 334 endif