1## Copyright (C) 2001, 2006, 2007, 2008, 2009 Laurent Mazet
2## Copyright (C) 2006 John W. Eaton
4## This file is part of Octave.
6## Octave is free software; you can redistribute it and/or modify it
7## under the terms of the GNU General Public License as published by
8## the Free Software Foundation; either version 3 of the License, or (at
9## your option) any later version.
11## Octave is distributed in the hope that it will be useful, but
12## WITHOUT ANY WARRANTY; without even the implied warranty of
13## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14## General Public License for more details.
16## You should have received a copy of the GNU General Public License
17## along with Octave; see the file COPYING. If not, see
18## <http://www.gnu.org/licenses/>.
21## @deftypefn {Function File} {} legend (@var{st1}, @var{st2}, @dots{})
22## @deftypefnx {Function File} {} legend (@var{st1}, @var{st2}, @dots{}, "location", @var{pos})
23## @deftypefnx {Function File} {} legend (@var{matstr})
24## @deftypefnx {Function File} {} legend (@var{matstr}, "location", @var{pos})
25## @deftypefnx {Function File} {} legend (@var{cell})
26## @deftypefnx {Function File} {} legend (@var{cell}, "location", @var{pos})
27## @deftypefnx {Function File} {} legend ('@var{func}')
29## Display a legend for the current axes using the specified strings
30## as labels. Legend entries may be specified as individual character
31## string arguments, a character array, or a cell array of character
32## strings. Legend works on line graphs, bar graphs, etc. A plot must
33## exist before legend is called.
35## The optional parameter @var{pos} specifies the location of the legend
38## @multitable @columnfractions 0.06 0.14 0.80
39## @item @tab north @tab
41## @item @tab south @tab
43## @item @tab east @tab
45## @item @tab west @tab
47## @item @tab northeast @tab
49## @item @tab northwest @tab
51## @item @tab southeast @tab
53## @item @tab southwest @tab
56## @item @tab outside @tab
57## can be appended to any location string
60## Some specific functions are directly available using @var{func}:
64## Show legends from the plot
67## Hide legends from the plot
69## Draw a box around legends
71## Withdraw the box around legends
73## Text is to the left of the keys
75## Text is to the right of the keys
79function legend (varargin)
81 [ca, varargin, nargin] = __plt_get_axis_arg__ ("legend", varargin{:});
85 pos = varargin{nargs};
86 if (isnumeric (pos) && isscalar (pos) && round (pos) == pos)
87 if (pos >= -1 && pos <= 4)
88 set (ca, "keypos", pos);
91 error ("legend: invalid position specified");
97 pos = varargin{nargs-1};
98 str = varargin{nargs};
99 if (strcmpi (pos, "location") && ischar (str))
100 set (ca, "keypos", str);
105 kids = get (ca, "children");
106 nkids = numel (kids);
108 turn_on_legend = false;
114 str = tolower (deblank (arg));
117 set (ca, "key", "off");
120 set (ca, "key", "on");
123 val = get (ca, "key");
124 if (strcmpi (val, "on"))
125 set (ca, "key", "off");
127 set (ca, "key", "on");
131 set (ca, "key", "on", "keybox", "on");
134 set (ca, "keybox", "off");
137 set (ca, "keyreverse", "off")
140 set (ca, "keyreverse", "on")
145 varargin = cellstr (arg);
146 nargs = numel (varargin);
148 elseif (iscellstr (arg))
150 nargs = numel (varargin);
152 error ("legend: expecting argument to be a character string");
159 typ = get (kids(k), "type");
160 if (strcmp (typ, "line") || strcmp (typ, "surface")
161 || strcmp (typ, "patch") || strcmp (typ, "hggroup"))
167 warning ("legend: plot data is empty; setting key labels has no effect");
176 typ = get (kids(k), "type");
178 && ! (strcmp (typ, "line") || strcmp (typ, "surface")
179 || strcmp (typ, "patch") || strcmp (typ, "hggroup")))
180 typ = get (kids(--k), "type");
183 if (strcmp (get (kids(k), "type"), "hggroup"))
184 hgkids = get (kids(k), "children");
185 for j = 1 : length (hgkids)
186 hgobj = get (hgkids (j));
187 if (isfield (hgobj, "keylabel"))
188 set (hgkids(j), "keylabel", arg);
193 set (kids(k), "keylabel", arg);
195 turn_on_legend = true;
201 warning ("legend: ignoring extra labels");
204 error ("legend: expecting argument to be a character string");
209 set (ca, "key", "on");
216%! plot(1:10, 1:10, 1:10, fliplr(1:10));
217%! title("incline is blue and decline is green");
218%! legend({"I'm blue", "I'm green"}, "location", "east")
222%! plot(1:10, 1:10, 1:10, fliplr(1:10));
223%! title("incline is blue and decline is green");
224%! legend("I'm blue", "I'm green", "location", "east")
229%! title("a very long label can sometimes cause problems");
230%! legend({"hello world"}, "location", "northeastoutside")
235%! title("a very long label can sometimes cause problems");
236%! legend("hello world", "location", "northeastoutside")
242%! h = plot(1:100, i + rand(100,1)); hold on;
243%! set (h, "color", get (gca, "colororder")(i,:))
244%! labels = {labels{:}, cstrcat("Signal ", num2str(i))};
246%! title("Signals with random offset and uniform noise")
247%! xlabel("Sample Nr [k]"); ylabel("Amplitude [V]");
248%! legend(labels, "location", "southoutside")
255%! h = plot(1:100, i + rand(100,1)); hold on;
256%! set (h, "color", get (gca, "colororder")(i,:))
257%! labels = {labels{:}, cstrcat("Signal ", num2str(i))};
259%! title("Signals with random offset and uniform noise")
260%! xlabel("Sample Nr [k]"); ylabel("Amplitude [V]");
261%! legend(labels{:}, "location", "southoutside")
266%! x = linspace (0, 10);
269%! stem (x, x.^2, 'g')
275%! x = linspace (0, 10);
276%! plot (x, x, x, x.^2);
281%! x = linspace (0, 10);
282%! plot (x, x, x, x.^2);
283%! legend ("linear", "quadratic");
289%! legend ({"1st Bar", "2nd Bar", "3rd Bar"})
295%! legend ("1st Bar", "2nd Bar", "3rd Bar")