changelog shortlog tags changeset files revisions annotate raw

scripts/plot/colorbar.m

changeset 10289: 4b124317dc38
parent:0d9f925b9705
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (67 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 2008, 2009 David Bateman
2##
3## This file is part of Octave.
4##
5## Octave is free software; you can redistribute it and/or modify it
6## under the terms of the GNU General Public License as published by
7## the Free Software Foundation; either version 3 of the License, or (at
8## your option) any later version.
9##
10## Octave is distributed in the hope that it will be useful, but
11## WITHOUT ANY WARRANTY; without even the implied warranty of
12## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13## General Public License for more details.
14##
15## You should have received a copy of the GNU General Public License
16## along with Octave; see the file COPYING. If not, see
17## <http://www.gnu.org/licenses/>.
18
19## -*- texinfo -*-
20## @deftypefn {Function File} {} colorbar (@var{s})
21## @deftypefnx {Function File} {} colorbar ("peer", @var{h}, @dots{})
22## Adds a colorbar to the current axes. Valid values for @var{s} are
23##
24## @table @asis
25## @item "EastOutside"
26## Place the colorbar outside the plot to the right. This is the default.
27## @item "East"
28## Place the colorbar inside the plot to the right.
29## @item "WestOutside"
30## Place the colorbar outside the plot to the left.
31## @item "West"
32## Place the colorbar inside the plot to the left.
33## @item "NorthOutside"
34## Place the colorbar above the plot.
35## @item "North"
36## Place the colorbar at the top of the plot.
37## @item "SouthOutside"
38## Place the colorbar under the plot.
39## @item "South"
40## Place the colorbar at the bottom of the plot.
41## @item "Off", "None"
42## Remove any existing colorbar from the plot.
43## @end table
44##
45## If the argument "peer" is given, then the following argument is treated
46## as the axes handle on which to add the colorbar.
47## @end deftypefn
48
49function h = colorbar (varargin)
50 ax = [];
51 loc = "eastoutside";
52 args = {};
53 deleting = false;
54
55 i = 1;
56 while (i <= nargin)
57 arg = varargin {i++};
58
59 if (ischar(arg))
60 if (strcmpi (arg, "peer"))
61 if (i > nargin)
62 error ("colorbar: missing axes handle after \"peer\"");
63 else
64 ax = vargin{i++}
65 if (!isscalar (ax) || ! ishandle (ax)
66 || strcmp (get (ax, "type"), "axes"))
67 error ("colorbar: expecting an axes handle following \"peer\"");
68 endif
69 endif
70 elseif (strcmpi (arg, "north") || strcmpi (arg, "south")
71 || strcmpi (arg, "east") || strcmpi (arg, "west")
72 || strcmpi (arg, "northoutside") || strcmpi (arg, "southoutside")
73 || strcmpi (arg, "eastoutside") || strcmpi (arg, "westoutside"))
74 loc = tolower (arg);
75 elseif (strcmpi (arg, "off") || strcmpi (arg, "none"))
76 deleting = true;
77 else
78 args{end+1} = arg;
79 endif
80 else
81 args{end+1} = arg;
82 endif
83 endwhile
84
85 if (isempty (ax))
86 ax = gca ();
87 endif
88
89 showhiddenhandles = get (0, "showhiddenhandles");
90 unwind_protect
91 set (0, "showhiddenhandles", "on")
92 cax = findobj (get (ax, "parent"), "tag", "colorbar", "type", "axes", "axes", ax);
93 if (! isempty (cax))
94 delete (cax);
95 endif
96 unwind_protect_cleanup
97 set (0, "showhiddenhandles", showhiddenhandles)
98 end_unwind_protect
99
100 if (! deleting)
101 obj = get (ax);
102 position = obj.position;
103 clen = rows (get (get (ax, "parent"), "colormap"));
104 cext = get (ax, "clim");
105 cdiff = (cext(2) - cext(1)) / clen / 2;
106 cmin = cext(1) + cdiff;
107 cmax = cext(2) - cdiff;
108
109 orig_pos = obj.position;
110 orig_opos = obj.outerposition;
111 [pos, cpos, vertical, mirror, aspect] = ...
112 __position_colorbox__ (loc, obj, ancestor (ax, "figure"));
113 set (ax, "activepositionproperty", "position", "position", pos);
114
115 cax = __go_axes__ (get (ax, "parent"), "tag", "colorbar",
116 "handlevisibility", "on",
117 "activepositionproperty", "position",
118 "position", cpos);
119 addproperty ("location", cax, "radio",
120 "eastoutside|east|westoutside|west|northoutside|north|southoutside|south",
121 loc);
122 addproperty ("axes", cax, "handle", ax);
123
124 if (vertical)
125 hi = image (cax, [0,1], [cmin, cmax], [1 : clen]');
126 if (mirror)
127 set (cax, "xtick", [], "xdir", "normal", "ydir", "normal",
128 "ylim", cext, "ylimmode", "manual",
129 "yaxislocation", "right", args{:});
130 else
131 set (cax, "xtick", [], "xdir", "normal", "ydir", "normal",
132 "ylim", cext, "ylimmode", "manual",
133 "yaxislocation", "left", args{:});
134 endif
135 else
136 hi = image (cax, [cmin, cmax], [0,1], [1 : clen]);
137 if (mirror)
138 set (cax, "ytick", [], "xdir", "normal", "ydir", "normal",
139 "xlim", cext, "xlimmode", "manual",
140 "xaxislocation", "top", args{:});
141 else
142 set (cax, "ytick", [], "xdir", "normal", "ydir", "normal",
143 "xlim", cext, "xlimmode", "manual",
144 "xaxislocation", "bottom", args{:});
145 endif
146 endif
147
148 if (! isnan (aspect))
149 set (cax, "plotboxaspectratio", aspect);
150 endif
151
152 ctext = text (0, 0, "", "tag", "colorbar","visible", "off",
153 "handlevisibility", "off", "xliminclude", "off",
154 "yliminclude", "off", "zliminclude", "off",
155 "deletefcn", {@deletecolorbar, cax, orig_pos, orig_opos});
156
157 set (cax, "deletefcn", {@resetaxis, orig_pos, orig_opos});
158
159 addlistener (ax, "clim", {@update_colorbar_clim, hi, vertical})
160 addlistener (ax, "plotboxaspectratio", {@update_colorbar_axis, cax})
161 addlistener (ax, "position", {@update_colorbar_axis, cax})
162
163 endif
164
165 if (nargout > 0)
166 h = cax;
167 endif
168endfunction
169
170function deletecolorbar (h, d, hc, pos, opos)
171 ## Don't delete the colorbar and reset the axis size if the
172 ## parent figure is being deleted.
173 if (ishandle (hc) && strcmp (get (hc, "type"), "axes") &&
174 (isempty (gcbf()) || strcmp (get (gcbf(), "beingdeleted"),"off")))
175 if (strcmp (get (hc, "beingdeleted"), "off"))
176 delete (hc);
177 endif
178 if (!isempty (ancestor (h, "axes")) &&
179 strcmp (get (ancestor (h, "axes"), "beingdeleted"), "off"))
180 set (ancestor (h, "axes"), "position", pos, "outerposition", opos);
181 endif
182 endif
183endfunction
184
185function resetaxis (h, d, pos, opos)
186 if (ishandle (h) && strcmp (get (h, "type"), "axes") &&
187 (isempty (gcbf()) || strcmp (get (gcbf(), "beingdeleted"),"off")) &&
188 ishandle (get (h, "axes")))
189 set (get (h, "axes"), "position", pos, "outerposition", opos);
190 endif
191endfunction
192
193function update_colorbar_clim (h, d, hi, vert)
194 if (ishandle (h) && strcmp (get (h, "type"), "image") &&
195 (isempty (gcbf()) || strcmp (get (gcbf(), "beingdeleted"),"off")))
196 clen = rows (get (get (h, "parent"), "colormap"));
197 cext = get (h, "clim");
198 cdiff = (cext(2) - cext(1)) / clen / 2;
199 cmin = cext(1) + cdiff;
200 cmax = cext(2) - cdiff;
201
202 if (vert)
203 set (hi, "ydata", [cmin, cmax]);
204 set (get (hi, "parent"), "ylim", cext);
205 else
206 set (hi, "xdata", [cmin, cmax]);
207 set (get (hi, "parent"), "xlim", cext);
208 endif
209 endif
210endfunction
211
212function update_colorbar_axis (h, d, cax)
213 if (ishandle (cax) && strcmp (get (cax, "type"), "axes") &&
214 (isempty (gcbf()) || strcmp (get (gcbf(), "beingdeleted"),"off")))
215 loc = get (cax, "location");
216 obj = get (h);
217 [pos, cpos, vertical, mirror, aspect] = ...
218 __position_colorbox__ (loc, obj, ancestor (h, "figure"));
219
220 if (vertical)
221 if (mirror)
222 set (cax, "xtick", [], "xdir", "normal", "ydir", "normal",
223 "yaxislocation", "right", "position", cpos);
224 else
225 set (cax, "xtick", [], "xdir", "normal", "ydir", "normal",
226 "yaxislocation", "left", "position", cpos);
227 endif
228 else
229 if (mirror)
230 set (cax, "ytick", [], "xdir", "normal", "ydir", "normal",
231 "xaxislocation", "top", "position", cpos);
232 else
233 set (cax, "ytick", [], "xdir", "normal", "ydir", "normal",
234 "xaxislocation", "bottom", "position", cpos);
235 endif
236 endif
237
238 if (! isnan (aspect))
239 set (cax, "plotboxaspectratio", aspect);
240 endif
241 endif
242endfunction
243
244function [pos, cpos, vertical, mirr, aspect] = __position_colorbox__ (cbox, obj, cf)
245
246 pos = obj.position;
247 sz = pos(3:4);
248
249 off = 0;
250 if (strcmpi (obj.plotboxaspectratiomode, "manual"))
251 r = obj.plotboxaspectratio;
252 if (pos(3) > pos(4))
253 switch (cbox)
254 case {"east", "eastoutside", "west", "westoutside"}
255 off = [(pos(3) - pos(4)) ./ (r(2) / r(1)), 0];
256 endswitch
257 else
258 switch (cbox)
259 case {"north", "northoutside", "south", "southoutside"}
260 off = [0, (pos(4) - pos(3)) ./ (r(1) / r(2))];
261 ## This shouldn't be here except that gnuplot doesn't have a
262 ## square window and so a square aspect ratio is not square.
263 ## The corrections are empirical.
264 if (strcmp (get (cf, "__backend__"), "gnuplot"))
265 if (length (cbox) > 7 && strcmp (cbox(end-6:end),"outside"))
266 off = off / 2;
267 else
268 off = off / 1.7;
269 endif
270 endif
271 endswitch
272 endif
273 off = off / 2;
274 endif
275
276 switch (cbox)
277 case "northoutside"
278 origin = pos(1:2) + [0., 0.9] .* sz + [1, -1] .* off;
279 sz = sz .* [1.0, 0.06];
280 pos(4) = 0.8 * pos(4);
281 mirr = true;
282 vertical = false;
283 case "north"
284 origin = pos(1:2) + [0.05, 0.9] .* sz + [1, -1] .* off;
285 sz = sz .* [1.0, 0.06] * 0.9;
286 mirr = false;
287 vertical = false;
288 case "southoutside"
289 origin = pos(1:2) + off;
290 sz = sz .* [1.0, 0.06];
291 pos(2) = pos(2) + pos(4) * 0.2;
292 pos(4) = 0.8 * pos(4);
293 mirr = false;
294 vertical = false;
295 case "south"
296 origin = pos(1:2) + [0.05, 0.05] .* sz + off;
297 sz = sz .* [1.0, 0.06] * 0.9;
298 mirr = true;
299 vertical = false;
300 case "eastoutside"
301 origin = pos(1:2) + [0.9, 0] .* sz + [-1, 1] .* off;
302 sz = sz .* [0.06, 1.0];
303 pos(3) = 0.8 * pos(3);
304 mirr = true;
305 vertical = true;
306 case "east"
307 origin = pos(1:2) + [0.9, 0.05] .* sz + [-1, 1] .* off;
308 sz = sz .* [0.06, 1.0] * 0.9;
309 mirr = false;
310 vertical = true;
311 case "westoutside"
312 origin = pos(1:2) + off;
313 sz = sz .* [0.06, 1.0];
314 pos(1) = pos(1) + pos(3) * 0.2;
315 pos(3) = 0.8 * pos(3);
316 mirr = false;
317 vertical = true;
318 case "west"
319 origin = pos(1:2) + [0.05, 0.05] .* sz + off;
320 sz = sz .* [0.06, 1.0] .* 0.9;
321 mirr = true;
322 vertical = true;
323 endswitch
324
325 cpos = [origin, sz];
326
327 if (strcmpi (obj.plotboxaspectratiomode, "manual"))
328 if (__gnuplot_has_feature__ ("screen_coordinates_for_{lrtb}margin"))
329 obj.position = pos;
330 actual_pos = __actual_axis_position__ (obj);
331 if (strfind (cbox, "outside"))
332 scale = 1.0;
333 else
334 scale = 0.9;
335 endif
336 if (sz(1) > sz(2))
337 dx = (1-scale)*actual_pos(3);
338 cpos(1) = actual_pos(1) + dx/2;
339 cpos(3) = actual_pos(3) - dx;
340 else
341 dy = (1-scale)*actual_pos(4);
342 cpos(2) = actual_pos(2) + dy/2;
343 cpos(4) = actual_pos(4) - dy;
344 endif
345 aspect = NaN;
346 else
347 if (vertical)
348 aspect = [1, 0.21, 1];
349 else
350 aspect = [0.21, 1, 1];
351 endif
352 endif
353 else
354 aspect = NaN;
355 endif
356
357endfunction
358
359%!demo
360%! clf
361%! n = 64; x = kron (1:n,ones(n,1)); x = abs(x - x.');
362%! imagesc(x)
363%! colorbar();
364
365%!demo
366%! clf
367%! n = 64; x = kron (1:n,ones(n,1)); x = abs(x - x.');
368%! imagesc(x)
369%! colorbar("westoutside");
370
371%!demo
372%! clf
373%! n = 64; x = kron (1:n,ones(n,1)); x = abs(x - x.');
374%! imagesc(x)
375%! colorbar("northoutside");
376
377%!demo
378%! clf
379%! n = 64; x = kron (1:n,ones(n,1)); x = abs(x - x.');
380%! imagesc(x)
381%! colorbar("southoutside");
382
383%!demo
384%! clf
385%! contour(peaks())
386%! colorbar("west");
387
388%!demo
389%! clf
390%! subplot(2,2,1)
391%! contour(peaks())
392%! colorbar("east");
393%! subplot(2,2,2)
394%! contour(peaks())
395%! colorbar("west");
396%! subplot(2,2,3)
397%! contour(peaks())
398%! colorbar("north");
399%! subplot(2,2,4)
400%! contour(peaks())
401%! colorbar("south");
402
403%!demo
404%! clf
405%! n = 64; x = kron (1:n,ones(n,1)); x = abs(x - x.');
406%! subplot(2,2,1)
407%! imagesc(x)
408%! colorbar();
409%! subplot(2,2,2)
410%! imagesc(x)
411%! colorbar("westoutside");
412%! subplot(2,2,3)
413%! imagesc(x)
414%! colorbar("northoutside");
415%! subplot(2,2,4)
416%! imagesc(x)
417%! colorbar("southoutside");
418
419%!demo
420%! clf
421%! n = 64; x = kron (1:n,ones(n,1)); x = abs(x - x.');
422%! subplot(1,2,1)
423%! imagesc(x)
424%! axis square;
425%! colorbar();
426%! subplot(1,2,2)
427%! imagesc(x)
428%! axis square;
429%! colorbar("westoutside");
430
431%!demo
432%! clf
433%! n = 64; x = kron (1:n,ones(n,1)); x = abs(x - x.');
434%! subplot(1,2,1)
435%! imagesc(x)
436%! axis square;
437%! colorbar("northoutside");
438%! subplot(1,2,2)
439%! imagesc(x)
440%! axis square;
441%! colorbar("southoutside");
442
443%!demo
444%! clf
445%! n = 64; x = kron (1:n,ones(n,1)); x = abs(x - x.');
446%! subplot(2,1,1)
447%! imagesc(x)
448%! axis square;
449%! colorbar();
450%! subplot(2,1,2)
451%! imagesc(x)
452%! axis square;
453%! colorbar("westoutside");
454
455%!demo
456%! clf
457%! n = 64; x = kron (1:n,ones(n,1)); x = abs(x - x.');
458%! subplot(2,1,1)
459%! imagesc(x)
460%! axis square;
461%! colorbar("northoutside");
462%! subplot(2,1,2)
463%! imagesc(x)
464%! axis square;
465%! colorbar("southoutside");
466
467%!demo
468%! clf
469%! n = 64; x = kron (1:n,ones(n,1)); x = abs(x - x.');
470%! subplot(1,2,1)
471%! imagesc(x)
472%! colorbar();
473%! subplot(1,2,2)
474%! imagesc(x)
475%! colorbar("westoutside");
476
477%!demo
478%! clf
479%! n = 64; x = kron (1:n,ones(n,1)); x = abs(x - x.');
480%! subplot(1,2,1)
481%! imagesc(x)
482%! colorbar("northoutside");
483%! subplot(1,2,2)
484%! imagesc(x)
485%! colorbar("southoutside");
486
487%!demo
488%! clf
489%! n = 64; x = kron (1:n,ones(n,1)); x = abs(x - x.');
490%! subplot(2,1,1)
491%! imagesc(x)
492%! colorbar();
493%! subplot(2,1,2)
494%! imagesc(x)
495%! colorbar("westoutside");
496
497%!demo
498%! clf
499%! n = 64; x = kron (1:n,ones(n,1)); x = abs(x - x.');
500%! subplot(2,1,1)
501%! imagesc(x)
502%! colorbar("northoutside");
503%! subplot(2,1,2)
504%! imagesc(x)
505%! colorbar("southoutside");
506
507%!demo
508%! clf
509%! n = 64; x = kron (1:n,ones(n,1)); x = abs(x - x.');
510%! subplot(1,2,1)
511%! contour(x)
512%! axis square;
513%! colorbar("east");
514%! xlim ([1, 64])
515%! ylim ([1, 64])
516%! subplot(1,2,2)
517%! contour(x)
518%! colorbar("west");
519%! xlim ([1, 64])
520%! ylim ([1, 64])
521
522%!demo
523%! clf
524%! n = 64; x = kron (1:n,ones(n,1)); x = abs(x - x.');
525%! contour (x)
526%! xlim ([1, 64])
527%! ylim ([1, 64])
528%! colorbar ();
529%! colorbar off
530
531%!demo
532%! clf
533%! n = 64; x = kron (1:n,ones(n,1)); x = abs(x - x.');
534%! contour (x)
535%! xlim ([1, 64])
536%! ylim ([1, 64])
537%! colorbar ();
538%! colorbar ();
539
540%!demo
541%! clf
542%! imagesc (1./hilb(99));
543%! h = colorbar;
544%! set (h, 'yscale', 'log');
545
546%!demo
547%! clf
548%! imagesc (log10 (1 ./ hilb (99)));
549%! h = colorbar;
550%! ytick = get(h, "ytick");
551%! set (h, "yticklabel", sprintf ('10^{%g}|', ytick));
552
553%!demo
554%! clf
555%! n=5;x=linspace(0,5,n);y=linspace(0,1,n);
556%! imagesc(1./hilb(n)); axis equal; colorbar
557
558%!demo
559%! clf
560%! n=5;x=linspace(0,5,n);y=linspace(0,1,n);
561%! imagesc(x,y,1./hilb(n)); axis equal; colorbar
562
563%!demo
564%! clf
565%! n=5;x=linspace(0,5,n);y=linspace(0,1,n);
566%! imagesc(y,x,1./hilb(n)); axis equal; colorbar
567## This requires that the axes position be properly determined for "axes equal"
568
569%!demo
570%! clf
571%! axes
572%! colorbar
573%! hold on
574%! contour(peaks)
575%! hold off
576
577%!demo
578%! clf
579%! plot([0, 2])
580%! colorbar ("east")
581%! axis square
582
583%!demo
584%! clf
585%! plot([0, 2])
586%! colorbar ("eastoutside")
587%! axis square
588
589%!demo
590%! clf
591%! plot([0, 2])
592%! colorbar ("east")
593%! axis equal
594
595%!demo
596%! clf
597%! plot([0, 2])
598%! colorbar ("eastoutside")
599%! axis equal