6146
|
1 ## Copyright (C) 2001 Laurent Mazet |
|
2 ## Copyright (C) 2006 John W. Eaton |
|
3 ## |
6440
|
4 ## This file is part of Octave. |
|
5 ## |
|
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 2, or (at your option) |
|
9 ## any later version. |
|
10 ## |
|
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. |
6146
|
15 ## |
|
16 ## You should have received a copy of the GNU General Public License |
6440
|
17 ## along with Octave; see the file COPYING. If not, write to the Free |
|
18 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
19 ## 02110-1301, USA. |
6146
|
20 |
|
21 ## -*- texinfo -*- |
6450
|
22 ## @deftypefn {Function File} {} legend (@var{st1}, @var{st2}, @dots{}) |
|
23 ## @deftypefnx {Function File} {} legend (@var{st1}, @var{st2}, @dots{}, @var{pos}) |
6146
|
24 ## @deftypefnx {Function File} {} legend (@var{matstr}) |
|
25 ## @deftypefnx {Function File} {} legend (@var{matstr}, @var{pos}) |
|
26 ## @deftypefnx {Function File} {} legend (@var{cell}) |
|
27 ## @deftypefnx {Function File} {} legend (@var{cell}, @var{pos}) |
|
28 ## @deftypefnx {Function File} {} legend ('@var{func}') |
|
29 ## |
|
30 ## Legend puts a legend on the current plot using the specified strings |
6450
|
31 ## as labels. Use independant strings (@var{st1}, @var{st2}, @dots{}), a |
6146
|
32 ## matrix of strings (@var{matstr}), or a cell array of strings (@var{cell}) to |
6450
|
33 ## specify legends. Legend works on line graphs, bar graphs, etc. |
6146
|
34 ## Be sure to call plot before calling legend. |
|
35 ## |
|
36 ## @var{pos} optionally places the legend in the specified location: |
|
37 ## |
|
38 ## @multitable @columnfractions 0.1 0.1 0.8 |
6395
|
39 ## @item @tab -1 @tab |
|
40 ## To the top right of the plot |
6146
|
41 ## @item @tab 0 @tab |
|
42 ## Don't move the legend box (default) |
|
43 ## @item @tab 1 @tab |
|
44 ## Upper right-hand corner |
|
45 ## @item @tab 2 @tab |
|
46 ## Upper left-hand corner |
|
47 ## @item @tab 3 @tab |
|
48 ## Lower left-hand corner |
|
49 ## @item @tab 4 @tab |
|
50 ## Lower right-hand corner |
|
51 ## @end multitable |
|
52 ## |
|
53 ## Some specific functions are directely avaliable using @var{func}: |
|
54 ## |
|
55 ## @table @code |
|
56 ## @item show |
|
57 ## Show legends from the plot |
|
58 ## @item hide |
|
59 ## @itemx off |
|
60 ## Hide legends from the plot |
|
61 ## @item boxon |
|
62 ## Draw a box around legends |
|
63 ## @item boxoff |
|
64 ## Withdraw the box around legends |
|
65 ## @item left |
|
66 ## Text is to the left of the keys |
|
67 ## @item right |
|
68 ## Text is to the right of the keys |
|
69 ## @end table |
|
70 ## @end deftypefn |
|
71 |
|
72 ## PKG_ADD mark_as_command legend |
|
73 |
|
74 function legend (varargin) |
|
75 |
6257
|
76 nargs = nargin; |
6146
|
77 |
6257
|
78 ca = gca (); |
6146
|
79 |
|
80 if (nargs > 0) |
|
81 pos = varargin{nargs}; |
|
82 if (isnumeric (pos) && isscalar (pos) && round (pos) == pos) |
6395
|
83 if (pos >= -1 && pos <= 4) |
6257
|
84 set (ca, "keypos", pos); |
6146
|
85 nargs--; |
|
86 else |
|
87 error ("legend: invalid position specified"); |
|
88 endif |
|
89 endif |
|
90 endif |
|
91 |
6257
|
92 kids = get (ca, "children"); |
|
93 nkids = numel (kids); |
|
94 k = 1; |
|
95 turn_on_legend = false; |
|
96 |
6146
|
97 if (nargs == 1) |
|
98 arg = varargin{1}; |
|
99 if (ischar (arg)) |
|
100 if (rows (arg) == 1) |
|
101 str = tolower (deblank (arg)); |
|
102 switch (str) |
|
103 case {"off", "hide"} |
6257
|
104 set (ca, "key", "off"); |
6146
|
105 case "show" |
6257
|
106 set (ca, "key", "on"); |
6146
|
107 case "toggle" |
6257
|
108 val = get (ca, "key"); |
|
109 if (strcmp (val, "on")) |
|
110 set (ca, "key", "off"); |
|
111 else |
|
112 set (ca, "key", "on"); |
|
113 endif |
6146
|
114 case "boxon" |
6257
|
115 set (ca, "key", "on", "keybox", "on"); |
6146
|
116 case "boxoff" |
6257
|
117 set (ca, "keybox", "off"); |
6146
|
118 otherwise |
6257
|
119 while (k <= nkids && ! strcmp (get (kids(k), "type"), "line")) |
|
120 k++; |
|
121 endwhile |
|
122 if (k <= nkids) |
|
123 turn_on_legend = true; |
|
124 set (kids(k), "keylabel", arg); |
|
125 else |
|
126 warning ("legend: ignoring extra labels"); |
|
127 endif |
6146
|
128 endswitch |
|
129 nargs--; |
|
130 else |
|
131 varargin = cellstr (arg); |
6740
|
132 nargs = numel (varargin); |
6146
|
133 endif |
|
134 elseif (iscellstr (arg)) |
|
135 varargin = arg; |
|
136 nargs = numel (varargin); |
|
137 else |
|
138 error ("legend: expecting argument to be a character string"); |
|
139 endif |
|
140 endif |
|
141 |
6272
|
142 if (nargs > 0) |
|
143 have_data = false; |
|
144 for i = 1:nkids |
|
145 if (strcmp (get (kids(k), "type"), "line")) |
|
146 have_data = true; |
|
147 break; |
|
148 endif |
|
149 endfor |
|
150 if (! have_data) |
|
151 warning ("legend: plot data is empty; setting key labels has no effect"); |
|
152 endif |
6147
|
153 endif |
|
154 |
6575
|
155 warned = false; |
6146
|
156 for i = 1:nargs |
|
157 arg = varargin{i}; |
|
158 if (ischar (arg)) |
6257
|
159 while (k <= nkids && ! strcmp (get (kids(k), "type"), "line")) |
|
160 k++; |
|
161 endwhile |
|
162 if (k <= nkids) |
|
163 set (kids(k), "keylabel", arg); |
|
164 turn_on_legend = true; |
6450
|
165 k++; |
6257
|
166 elseif (! warned) |
|
167 warned = true; |
|
168 warning ("legend: ignoring extra labels"); |
|
169 endif |
6146
|
170 else |
|
171 error ("legend: expecting argument to be a character string"); |
|
172 endif |
|
173 endfor |
|
174 |
6257
|
175 if (turn_on_legend) |
|
176 set (ca, "key", "on"); |
6146
|
177 endif |
|
178 |
|
179 endfunction |
|
180 |
|
181 %!demo |
|
182 %! close all; |
|
183 %! plot(1:10, 1:10); |
|
184 %! title("a very long label can sometimes cause problems"); |
|
185 %! legend({"hello world"}, -1) |
|
186 |
|
187 %!demo |
|
188 %! close all; |
|
189 %! labels = {}; |
|
190 %! for i = 1:5 |
|
191 %! plot(1:100, i + rand(100,1)); hold on; |
|
192 %! labels = {labels{:}, strcat("Signal ", num2str(i))}; |
|
193 %! endfor; hold off; |
|
194 %! title("Signals with random offset and uniform noise") |
|
195 %! xlabel("Sample Nr [k]"); ylabel("Amplitude [V]"); |
|
196 %! legend(labels, -1) |
|
197 %! legend("boxon") |