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
3## This file is part of Octave.
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.
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.
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/>.
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
26## Place the colorbar outside the plot to the right. This is the default.
28## Place the colorbar inside the plot to the right.
30## Place the colorbar outside the plot to the left.
32## Place the colorbar inside the plot to the left.
33## @item "NorthOutside"
34## Place the colorbar above the plot.
36## Place the colorbar at the top of the plot.
37## @item "SouthOutside"
38## Place the colorbar under the plot.
40## Place the colorbar at the bottom of the plot.
42## Remove any existing colorbar from the plot.
45## If the argument "peer" is given, then the following argument is treated
46## as the axes handle on which to add the colorbar.
49function h = colorbar (varargin)
60 if (strcmpi (arg, "peer"))
62 error ("colorbar: missing axes handle after \"peer\"");
65 if (!isscalar (ax) || ! ishandle (ax)
66 || strcmp (get (ax, "type"), "axes"))
67 error ("colorbar: expecting an axes handle following \"peer\"");
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"))
75 elseif (strcmpi (arg, "off") || strcmpi (arg, "none"))
89 showhiddenhandles = get (0, "showhiddenhandles");
91 set (0, "showhiddenhandles", "on")
92 cax = findobj (get (ax, "parent"), "tag", "colorbar", "type", "axes", "axes", ax);
96 unwind_protect_cleanup
97 set (0, "showhiddenhandles", showhiddenhandles)
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;
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);
115 cax = __go_axes__ (get (ax, "parent"), "tag", "colorbar",
116 "handlevisibility", "on",
117 "activepositionproperty", "position",
119 addproperty ("location", cax, "radio",
120 "eastoutside|east|westoutside|west|northoutside|north|southoutside|south",
122 addproperty ("axes", cax, "handle", ax);
125 hi = image (cax, [0,1], [cmin, cmax], [1 : clen]');
127 set (cax, "xtick", [], "xdir", "normal", "ydir", "normal",
128 "ylim", cext, "ylimmode", "manual",
129 "yaxislocation", "right", args{:});
131 set (cax, "xtick", [], "xdir", "normal", "ydir", "normal",
132 "ylim", cext, "ylimmode", "manual",
133 "yaxislocation", "left", args{:});
136 hi = image (cax, [cmin, cmax], [0,1], [1 : clen]);
138 set (cax, "ytick", [], "xdir", "normal", "ydir", "normal",
139 "xlim", cext, "xlimmode", "manual",
140 "xaxislocation", "top", args{:});
142 set (cax, "ytick", [], "xdir", "normal", "ydir", "normal",
143 "xlim", cext, "xlimmode", "manual",
144 "xaxislocation", "bottom", args{:});
148 if (! isnan (aspect))
149 set (cax, "plotboxaspectratio", aspect);
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});
157 set (cax, "deletefcn", {@resetaxis, orig_pos, orig_opos});
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})
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"))
178 if (!isempty (ancestor (h, "axes")) &&
179 strcmp (get (ancestor (h, "axes"), "beingdeleted"), "off"))
180 set (ancestor (h, "axes"), "position", pos, "outerposition", opos);
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);
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;
203 set (hi, "ydata", [cmin, cmax]);
204 set (get (hi, "parent"), "ylim", cext);
206 set (hi, "xdata", [cmin, cmax]);
207 set (get (hi, "parent"), "xlim", cext);
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");
217 [pos, cpos, vertical, mirror, aspect] = ...
218 __position_colorbox__ (loc, obj, ancestor (h, "figure"));
222 set (cax, "xtick", [], "xdir", "normal", "ydir", "normal",
223 "yaxislocation", "right", "position", cpos);
225 set (cax, "xtick", [], "xdir", "normal", "ydir", "normal",
226 "yaxislocation", "left", "position", cpos);
230 set (cax, "ytick", [], "xdir", "normal", "ydir", "normal",
231 "xaxislocation", "top", "position", cpos);
233 set (cax, "ytick", [], "xdir", "normal", "ydir", "normal",
234 "xaxislocation", "bottom", "position", cpos);
238 if (! isnan (aspect))
239 set (cax, "plotboxaspectratio", aspect);
244function [pos, cpos, vertical, mirr, aspect] = __position_colorbox__ (cbox, obj, cf)
250 if (strcmpi (obj.plotboxaspectratiomode, "manual"))
251 r = obj.plotboxaspectratio;
254 case {"east", "eastoutside", "west", "westoutside"}
255 off = [(pos(3) - pos(4)) ./ (r(2) / r(1)), 0];
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"))
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);
284 origin = pos(1:2) + [0.05, 0.9] .* sz + [1, -1] .* off;
285 sz = sz .* [1.0, 0.06] * 0.9;
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);
296 origin = pos(1:2) + [0.05, 0.05] .* sz + off;
297 sz = sz .* [1.0, 0.06] * 0.9;
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);
307 origin = pos(1:2) + [0.9, 0.05] .* sz + [-1, 1] .* off;
308 sz = sz .* [0.06, 1.0] * 0.9;
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);
319 origin = pos(1:2) + [0.05, 0.05] .* sz + off;
320 sz = sz .* [0.06, 1.0] .* 0.9;
327 if (strcmpi (obj.plotboxaspectratiomode, "manual"))
328 if (__gnuplot_has_feature__ ("screen_coordinates_for_{lrtb}margin"))
330 actual_pos = __actual_axis_position__ (obj);
331 if (strfind (cbox, "outside"))
337 dx = (1-scale)*actual_pos(3);
338 cpos(1) = actual_pos(1) + dx/2;
339 cpos(3) = actual_pos(3) - dx;
341 dy = (1-scale)*actual_pos(4);
342 cpos(2) = actual_pos(2) + dy/2;
343 cpos(4) = actual_pos(4) - dy;
348 aspect = [1, 0.21, 1];
350 aspect = [0.21, 1, 1];
361%! n = 64; x = kron (1:n,ones(n,1)); x = abs(x - x.');
367%! n = 64; x = kron (1:n,ones(n,1)); x = abs(x - x.');
369%! colorbar("westoutside");
373%! n = 64; x = kron (1:n,ones(n,1)); x = abs(x - x.');
375%! colorbar("northoutside");
379%! n = 64; x = kron (1:n,ones(n,1)); x = abs(x - x.');
381%! colorbar("southoutside");
405%! n = 64; x = kron (1:n,ones(n,1)); x = abs(x - x.');
411%! colorbar("westoutside");
414%! colorbar("northoutside");
417%! colorbar("southoutside");
421%! n = 64; x = kron (1:n,ones(n,1)); x = abs(x - x.');
429%! colorbar("westoutside");
433%! n = 64; x = kron (1:n,ones(n,1)); x = abs(x - x.');
437%! colorbar("northoutside");
441%! colorbar("southoutside");
445%! n = 64; x = kron (1:n,ones(n,1)); x = abs(x - x.');
453%! colorbar("westoutside");
457%! n = 64; x = kron (1:n,ones(n,1)); x = abs(x - x.');
461%! colorbar("northoutside");
465%! colorbar("southoutside");
469%! n = 64; x = kron (1:n,ones(n,1)); x = abs(x - x.');
475%! colorbar("westoutside");
479%! n = 64; x = kron (1:n,ones(n,1)); x = abs(x - x.');
482%! colorbar("northoutside");
485%! colorbar("southoutside");
489%! n = 64; x = kron (1:n,ones(n,1)); x = abs(x - x.');
495%! colorbar("westoutside");
499%! n = 64; x = kron (1:n,ones(n,1)); x = abs(x - x.');
502%! colorbar("northoutside");
505%! colorbar("southoutside");
509%! n = 64; x = kron (1:n,ones(n,1)); x = abs(x - x.');
524%! n = 64; x = kron (1:n,ones(n,1)); x = abs(x - x.');
533%! n = 64; x = kron (1:n,ones(n,1)); x = abs(x - x.');
542%! imagesc (1./hilb(99));
544%! set (h, 'yscale', 'log');
548%! imagesc (log10 (1 ./ hilb (99)));
550%! ytick = get(h, "ytick");
551%! set (h, "yticklabel", sprintf ('10^{%g}|', ytick));
555%! n=5;x=linspace(0,5,n);y=linspace(0,1,n);
556%! imagesc(1./hilb(n)); axis equal; colorbar
560%! n=5;x=linspace(0,5,n);y=linspace(0,1,n);
561%! imagesc(x,y,1./hilb(n)); axis equal; colorbar
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"
586%! colorbar ("eastoutside")
598%! colorbar ("eastoutside")