comparison scripts/plot/legend.m @ 10662:3afcd24ced61

legend.m: Key labels for specified objects.
author Ben Abbott <bpabbott@mac.com>
date Wed, 26 May 2010 19:52:40 -0400
parents 95c3e38098bf
children 7a43499f074a
comparison
equal deleted inserted replaced
10661:3df114a44da3 10662:3afcd24ced61
22 ## @deftypefnx {Function File} {} legend (@var{st1}, @var{st2}, @dots{}, "location", @var{pos}) 22 ## @deftypefnx {Function File} {} legend (@var{st1}, @var{st2}, @dots{}, "location", @var{pos})
23 ## @deftypefnx {Function File} {} legend (@var{matstr}) 23 ## @deftypefnx {Function File} {} legend (@var{matstr})
24 ## @deftypefnx {Function File} {} legend (@var{matstr}, "location", @var{pos}) 24 ## @deftypefnx {Function File} {} legend (@var{matstr}, "location", @var{pos})
25 ## @deftypefnx {Function File} {} legend (@var{cell}) 25 ## @deftypefnx {Function File} {} legend (@var{cell})
26 ## @deftypefnx {Function File} {} legend (@var{cell}, "location", @var{pos}) 26 ## @deftypefnx {Function File} {} legend (@var{cell}, "location", @var{pos})
27 ## @deftypefnx {Function File} {} legend (@var{hax}, @dots{})
28 ## @deftypefnx {Function File} {} legend (@var{hobjs}, @dots{})
29 ## @deftypefnx {Function File} {} legend (@var{hax}, @var{hobjs}, @dots{})
27 ## @deftypefnx {Function File} {} legend ('@var{func}') 30 ## @deftypefnx {Function File} {} legend ('@var{func}')
28 ## 31 ##
29 ## Display a legend for the current axes using the specified strings 32 ## Display a legend for the axes with handle @var{hax}, or the current axes,
30 ## as labels. Legend entries may be specified as individual character 33 ## using the specified strings as labels. Legend entries may be specified
31 ## string arguments, a character array, or a cell array of character 34 ## as individual character string arguments, a character array, or a cell
32 ## strings. Legend works on line graphs, bar graphs, etc. A plot must 35 ## array of character strings. If the handles, @var{hobjs}, are not specified
33 ## exist before legend is called. 36 ## the legend's strings will be associated with the axes' descendents.
37 ## Legend works on line graphs, bar graphs, etc.
38 ## A plot must exist before legend is called.
34 ## 39 ##
35 ## The optional parameter @var{pos} specifies the location of the legend 40 ## The optional parameter @var{pos} specifies the location of the legend
36 ## as follows: 41 ## as follows:
37 ## 42 ##
38 ## @multitable @columnfractions 0.06 0.14 0.80 43 ## @multitable @columnfractions 0.06 0.14 0.80
79 function legend (varargin) 84 function legend (varargin)
80 85
81 [ca, varargin, nargin] = __plt_get_axis_arg__ ("legend", varargin{:}); 86 [ca, varargin, nargin] = __plt_get_axis_arg__ ("legend", varargin{:});
82 nargs = nargin; 87 nargs = nargin;
83 88
89 if (all (ishandle (varargin{1})))
90 kids = flipud (varargin{1}(:));
91 varargin(1) = [];
92 nargs = numel (varargin);
93 else
94 kids = get (ca, "children");
95 endif
96 nkids = numel (kids);
97
84 if (nargs > 0) 98 if (nargs > 0)
85 pos = varargin{nargs}; 99 pos = varargin{nargs};
86 if (isnumeric (pos) && isscalar (pos) && round (pos) == pos) 100 if (isnumeric (pos) && isscalar (pos) && round (pos) == pos)
87 if (pos >= -1 && pos <= 4) 101 if (pos >= -1 && pos <= 4)
88 set (ca, "keypos", pos); 102 set (ca, "keypos", pos);
100 set (ca, "keypos", str); 114 set (ca, "keypos", str);
101 nargs -= 2; 115 nargs -= 2;
102 endif 116 endif
103 endif 117 endif
104 118
105 kids = get (ca, "children");
106 nkids = numel (kids);
107 k = 1; 119 k = 1;
108 turn_on_legend = false; 120 turn_on_legend = false;
109 121
110 if (nargs == 1) 122 if (nargs == 1)
111 arg = varargin{1}; 123 arg = varargin{1};
291 %!demo 303 %!demo
292 %! clf 304 %! clf
293 %! bar (rand (2, 3)) 305 %! bar (rand (2, 3))
294 %! ylim ([0 1.2]) 306 %! ylim ([0 1.2])
295 %! legend ("1st Bar", "2nd Bar", "3rd Bar") 307 %! legend ("1st Bar", "2nd Bar", "3rd Bar")
308
309 %!demo
310 %! clf
311 %! x = 0:0.1:7;
312 %! h = plot (x, sin(x), x, cos(x), x, sin(x.^2/10), x, cos(x.^2/10));
313 %! title ("Only the sin() objects have keylabels")
314 %! legend (h([1, 3]), {"sin(x)", "sin(x^2/10)"}, "location", "southwest")