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