Mercurial > hg > octave-nkf
annotate 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 |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2001, 2006, 2007, 2008, 2009 Laurent Mazet |
6146 | 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 | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
6440 | 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 | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
6146 | 19 |
20 ## -*- texinfo -*- | |
6450 | 21 ## @deftypefn {Function File} {} legend (@var{st1}, @var{st2}, @dots{}) |
6977 | 22 ## @deftypefnx {Function File} {} legend (@var{st1}, @var{st2}, @dots{}, "location", @var{pos}) |
6146 | 23 ## @deftypefnx {Function File} {} legend (@var{matstr}) |
6977 | 24 ## @deftypefnx {Function File} {} legend (@var{matstr}, "location", @var{pos}) |
6146 | 25 ## @deftypefnx {Function File} {} legend (@var{cell}) |
6977 | 26 ## @deftypefnx {Function File} {} legend (@var{cell}, "location", @var{pos}) |
10662
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
27 ## @deftypefnx {Function File} {} legend (@var{hax}, @dots{}) |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
28 ## @deftypefnx {Function File} {} legend (@var{hobjs}, @dots{}) |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
29 ## @deftypefnx {Function File} {} legend (@var{hax}, @var{hobjs}, @dots{}) |
6146 | 30 ## @deftypefnx {Function File} {} legend ('@var{func}') |
31 ## | |
10662
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
32 ## Display a legend for the axes with handle @var{hax}, or the current axes, |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
33 ## using the specified strings as labels. Legend entries may be specified |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
34 ## as individual character string arguments, a character array, or a cell |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
35 ## array of character strings. If the handles, @var{hobjs}, are not specified |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
36 ## the legend's strings will be associated with the axes' descendents. |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
37 ## Legend works on line graphs, bar graphs, etc. |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
38 ## A plot must exist before legend is called. |
6146 | 39 ## |
6895 | 40 ## The optional parameter @var{pos} specifies the location of the legend |
41 ## as follows: | |
6146 | 42 ## |
6977 | 43 ## @multitable @columnfractions 0.06 0.14 0.80 |
44 ## @item @tab north @tab | |
45 ## center top | |
46 ## @item @tab south @tab | |
47 ## center bottom | |
48 ## @item @tab east @tab | |
49 ## right center | |
50 ## @item @tab west @tab | |
51 ## left center | |
52 ## @item @tab northeast @tab | |
53 ## right top (default) | |
54 ## @item @tab northwest @tab | |
55 ## left top | |
56 ## @item @tab southeast @tab | |
57 ## right bottom | |
58 ## @item @tab southwest @tab | |
59 ## left bottom | |
60 ## @item | |
61 ## @item @tab outside @tab | |
62 ## can be appended to any location string | |
6146 | 63 ## @end multitable |
64 ## | |
7001 | 65 ## Some specific functions are directly available using @var{func}: |
6146 | 66 ## |
7148 | 67 ## @table @asis |
6895 | 68 ## @item "show" |
6146 | 69 ## Show legends from the plot |
6895 | 70 ## @item "hide" |
7148 | 71 ## @itemx "off" |
6146 | 72 ## Hide legends from the plot |
6895 | 73 ## @item "boxon" |
6146 | 74 ## Draw a box around legends |
6895 | 75 ## @item "boxoff" |
6146 | 76 ## Withdraw the box around legends |
6895 | 77 ## @item "left" |
6146 | 78 ## Text is to the left of the keys |
6895 | 79 ## @item "right" |
6146 | 80 ## Text is to the right of the keys |
81 ## @end table | |
82 ## @end deftypefn | |
83 | |
84 function legend (varargin) | |
85 | |
8208 | 86 [ca, varargin, nargin] = __plt_get_axis_arg__ ("legend", varargin{:}); |
6257 | 87 nargs = nargin; |
6146 | 88 |
10662
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
89 if (all (ishandle (varargin{1}))) |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
90 kids = flipud (varargin{1}(:)); |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
91 varargin(1) = []; |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
92 nargs = numel (varargin); |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
93 else |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
94 kids = get (ca, "children"); |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
95 endif |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
96 nkids = numel (kids); |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
97 |
6146 | 98 if (nargs > 0) |
99 pos = varargin{nargs}; | |
100 if (isnumeric (pos) && isscalar (pos) && round (pos) == pos) | |
6395 | 101 if (pos >= -1 && pos <= 4) |
10549 | 102 set (ca, "keypos", pos); |
103 nargs--; | |
6146 | 104 else |
10549 | 105 error ("legend: invalid position specified"); |
6146 | 106 endif |
107 endif | |
108 endif | |
6977 | 109 |
110 if (nargs > 1) | |
111 pos = varargin{nargs-1}; | |
7054 | 112 str = varargin{nargs}; |
6977 | 113 if (strcmpi (pos, "location") && ischar (str)) |
114 set (ca, "keypos", str); | |
115 nargs -= 2; | |
116 endif | |
117 endif | |
6146 | 118 |
6257 | 119 k = 1; |
120 turn_on_legend = false; | |
121 | |
6146 | 122 if (nargs == 1) |
123 arg = varargin{1}; | |
124 if (ischar (arg)) | |
125 if (rows (arg) == 1) | |
10549 | 126 str = tolower (deblank (arg)); |
127 switch (str) | |
128 case {"off", "hide"} | |
129 set (ca, "key", "off"); | |
130 nargs--; | |
131 case "show" | |
132 set (ca, "key", "on"); | |
133 nargs--; | |
134 case "toggle" | |
135 val = get (ca, "key"); | |
136 if (strcmpi (val, "on")) | |
137 set (ca, "key", "off"); | |
138 else | |
139 set (ca, "key", "on"); | |
140 endif | |
141 nargs--; | |
142 case "boxon" | |
143 set (ca, "key", "on", "keybox", "on"); | |
144 nargs--; | |
145 case "boxoff" | |
146 set (ca, "keybox", "off"); | |
147 nargs--; | |
148 case "left" | |
149 set (ca, "keyreverse", "off") | |
150 nargs--; | |
151 case "right" | |
152 set (ca, "keyreverse", "on") | |
153 nargs--; | |
154 otherwise | |
155 endswitch | |
6146 | 156 else |
10549 | 157 varargin = cellstr (arg); |
158 nargs = numel (varargin); | |
6146 | 159 endif |
160 elseif (iscellstr (arg)) | |
8529
774b44619c5c
Fix legend order for both horizontal and vertical string cell.
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
8343
diff
changeset
|
161 varargin = arg; |
6146 | 162 nargs = numel (varargin); |
163 else | |
164 error ("legend: expecting argument to be a character string"); | |
165 endif | |
166 endif | |
167 | |
6272 | 168 if (nargs > 0) |
169 have_data = false; | |
9451 | 170 for k = 1:nkids |
171 typ = get (kids(k), "type"); | |
172 if (strcmp (typ, "line") || strcmp (typ, "surface") | |
10549 | 173 || strcmp (typ, "patch") || strcmp (typ, "hggroup")) |
174 have_data = true; | |
175 break; | |
6272 | 176 endif |
177 endfor | |
178 if (! have_data) | |
179 warning ("legend: plot data is empty; setting key labels has no effect"); | |
180 endif | |
6147 | 181 endif |
182 | |
6575 | 183 warned = false; |
9451 | 184 k = nkids; |
185 for i = 1:nargs | |
6146 | 186 arg = varargin{i}; |
187 if (ischar (arg)) | |
9451 | 188 typ = get (kids(k), "type"); |
189 while (k > 1 | |
10549 | 190 && ! (strcmp (typ, "line") || strcmp (typ, "surface") |
191 || strcmp (typ, "patch") || strcmp (typ, "hggroup"))) | |
192 typ = get (kids(--k), "type"); | |
6257 | 193 endwhile |
9451 | 194 if (k > 0) |
10549 | 195 if (strcmp (get (kids(k), "type"), "hggroup")) |
196 hgkids = get (kids(k), "children"); | |
197 for j = 1 : length (hgkids) | |
198 hgobj = get (hgkids (j)); | |
199 if (isfield (hgobj, "keylabel")) | |
200 set (hgkids(j), "keylabel", arg); | |
201 break; | |
202 endif | |
203 endfor | |
204 else | |
205 set (kids(k), "keylabel", arg); | |
206 endif | |
207 turn_on_legend = true; | |
208 if (--k == 0) | |
209 break; | |
210 endif | |
6257 | 211 elseif (! warned) |
10549 | 212 warned = true; |
213 warning ("legend: ignoring extra labels"); | |
6257 | 214 endif |
6146 | 215 else |
216 error ("legend: expecting argument to be a character string"); | |
217 endif | |
218 endfor | |
219 | |
6257 | 220 if (turn_on_legend) |
221 set (ca, "key", "on"); | |
6146 | 222 endif |
223 | |
224 endfunction | |
225 | |
226 %!demo | |
8790
a013ff655ca4
Trivial changes to demos to produce a more pleasant output for octave+gnuplot+aquaterm.
Ben Abbott <bpabbott@mac.com>
parents:
8746
diff
changeset
|
227 %! clf |
8343
9f34f7636fe0
legend.m: Correct ording of legend labels.
Ben Abbott <bpabbott@mac.com>
parents:
8291
diff
changeset
|
228 %! plot(1:10, 1:10, 1:10, fliplr(1:10)); |
9f34f7636fe0
legend.m: Correct ording of legend labels.
Ben Abbott <bpabbott@mac.com>
parents:
8291
diff
changeset
|
229 %! title("incline is blue and decline is green"); |
9f34f7636fe0
legend.m: Correct ording of legend labels.
Ben Abbott <bpabbott@mac.com>
parents:
8291
diff
changeset
|
230 %! legend({"I'm blue", "I'm green"}, "location", "east") |
9f34f7636fe0
legend.m: Correct ording of legend labels.
Ben Abbott <bpabbott@mac.com>
parents:
8291
diff
changeset
|
231 |
9f34f7636fe0
legend.m: Correct ording of legend labels.
Ben Abbott <bpabbott@mac.com>
parents:
8291
diff
changeset
|
232 %!demo |
8790
a013ff655ca4
Trivial changes to demos to produce a more pleasant output for octave+gnuplot+aquaterm.
Ben Abbott <bpabbott@mac.com>
parents:
8746
diff
changeset
|
233 %! clf |
9451 | 234 %! plot(1:10, 1:10, 1:10, fliplr(1:10)); |
235 %! title("incline is blue and decline is green"); | |
236 %! legend("I'm blue", "I'm green", "location", "east") | |
237 | |
238 %!demo | |
239 %! clf | |
6146 | 240 %! plot(1:10, 1:10); |
241 %! title("a very long label can sometimes cause problems"); | |
6977 | 242 %! legend({"hello world"}, "location", "northeastoutside") |
6146 | 243 |
244 %!demo | |
8790
a013ff655ca4
Trivial changes to demos to produce a more pleasant output for octave+gnuplot+aquaterm.
Ben Abbott <bpabbott@mac.com>
parents:
8746
diff
changeset
|
245 %! clf |
9451 | 246 %! plot(1:10, 1:10); |
247 %! title("a very long label can sometimes cause problems"); | |
248 %! legend("hello world", "location", "northeastoutside") | |
249 | |
250 %!demo | |
251 %! clf | |
6146 | 252 %! labels = {}; |
253 %! for i = 1:5 | |
8915
485eabc0cfec
__go_draw_axes__.m: Preserve the order of axes' children when pruning the handles for the {x,y,z}labels and title.
Ben Abbott <bpabbott@mac.com>
parents:
8790
diff
changeset
|
254 %! h = plot(1:100, i + rand(100,1)); hold on; |
485eabc0cfec
__go_draw_axes__.m: Preserve the order of axes' children when pruning the handles for the {x,y,z}labels and title.
Ben Abbott <bpabbott@mac.com>
parents:
8790
diff
changeset
|
255 %! set (h, "color", get (gca, "colororder")(i,:)) |
7540
3422f39573b1
strcat.m: Matlab compatibility, with cstrcat.m replacing conventional strcat.m.
Ben Abbott <bpabbott@mac.com>
parents:
7148
diff
changeset
|
256 %! labels = {labels{:}, cstrcat("Signal ", num2str(i))}; |
6146 | 257 %! endfor; hold off; |
258 %! title("Signals with random offset and uniform noise") | |
259 %! xlabel("Sample Nr [k]"); ylabel("Amplitude [V]"); | |
6977 | 260 %! legend(labels, "location", "southoutside") |
6146 | 261 %! legend("boxon") |
9451 | 262 |
263 %!demo | |
264 %! clf | |
265 %! labels = {}; | |
266 %! for i = 1:5 | |
267 %! h = plot(1:100, i + rand(100,1)); hold on; | |
268 %! set (h, "color", get (gca, "colororder")(i,:)) | |
269 %! labels = {labels{:}, cstrcat("Signal ", num2str(i))}; | |
270 %! endfor; hold off; | |
271 %! title("Signals with random offset and uniform noise") | |
272 %! xlabel("Sample Nr [k]"); ylabel("Amplitude [V]"); | |
273 %! legend(labels{:}, "location", "southoutside") | |
274 %! legend("boxon") | |
275 | |
276 %!demo | |
9457
f9fb8c1a8e45
legend.m: Add an hggroup demo using bar(). Include clf() in each demo.
Ben Abbott <bpabbott@mac.com>
parents:
9451
diff
changeset
|
277 %! clf |
9451 | 278 %! x = linspace (0, 10); |
279 %! plot (x, x); | |
280 %! hold ("on"); | |
281 %! stem (x, x.^2, 'g') | |
282 %! legend ("linear"); | |
283 %! hold ("off"); | |
284 | |
285 %!demo | |
9457
f9fb8c1a8e45
legend.m: Add an hggroup demo using bar(). Include clf() in each demo.
Ben Abbott <bpabbott@mac.com>
parents:
9451
diff
changeset
|
286 %! clf |
9451 | 287 %! x = linspace (0, 10); |
288 %! plot (x, x, x, x.^2); | |
289 %! legend ("linear"); | |
290 | |
291 %!demo | |
9457
f9fb8c1a8e45
legend.m: Add an hggroup demo using bar(). Include clf() in each demo.
Ben Abbott <bpabbott@mac.com>
parents:
9451
diff
changeset
|
292 %! clf |
9451 | 293 %! x = linspace (0, 10); |
294 %! plot (x, x, x, x.^2); | |
295 %! legend ("linear", "quadratic"); | |
9457
f9fb8c1a8e45
legend.m: Add an hggroup demo using bar(). Include clf() in each demo.
Ben Abbott <bpabbott@mac.com>
parents:
9451
diff
changeset
|
296 |
f9fb8c1a8e45
legend.m: Add an hggroup demo using bar(). Include clf() in each demo.
Ben Abbott <bpabbott@mac.com>
parents:
9451
diff
changeset
|
297 %!demo |
f9fb8c1a8e45
legend.m: Add an hggroup demo using bar(). Include clf() in each demo.
Ben Abbott <bpabbott@mac.com>
parents:
9451
diff
changeset
|
298 %! clf |
f9fb8c1a8e45
legend.m: Add an hggroup demo using bar(). Include clf() in each demo.
Ben Abbott <bpabbott@mac.com>
parents:
9451
diff
changeset
|
299 %! bar (rand (2, 3)) |
f9fb8c1a8e45
legend.m: Add an hggroup demo using bar(). Include clf() in each demo.
Ben Abbott <bpabbott@mac.com>
parents:
9451
diff
changeset
|
300 %! ylim ([0 1.2]) |
f9fb8c1a8e45
legend.m: Add an hggroup demo using bar(). Include clf() in each demo.
Ben Abbott <bpabbott@mac.com>
parents:
9451
diff
changeset
|
301 %! legend ({"1st Bar", "2nd Bar", "3rd Bar"}) |
f9fb8c1a8e45
legend.m: Add an hggroup demo using bar(). Include clf() in each demo.
Ben Abbott <bpabbott@mac.com>
parents:
9451
diff
changeset
|
302 |
f9fb8c1a8e45
legend.m: Add an hggroup demo using bar(). Include clf() in each demo.
Ben Abbott <bpabbott@mac.com>
parents:
9451
diff
changeset
|
303 %!demo |
f9fb8c1a8e45
legend.m: Add an hggroup demo using bar(). Include clf() in each demo.
Ben Abbott <bpabbott@mac.com>
parents:
9451
diff
changeset
|
304 %! clf |
f9fb8c1a8e45
legend.m: Add an hggroup demo using bar(). Include clf() in each demo.
Ben Abbott <bpabbott@mac.com>
parents:
9451
diff
changeset
|
305 %! bar (rand (2, 3)) |
f9fb8c1a8e45
legend.m: Add an hggroup demo using bar(). Include clf() in each demo.
Ben Abbott <bpabbott@mac.com>
parents:
9451
diff
changeset
|
306 %! ylim ([0 1.2]) |
f9fb8c1a8e45
legend.m: Add an hggroup demo using bar(). Include clf() in each demo.
Ben Abbott <bpabbott@mac.com>
parents:
9451
diff
changeset
|
307 %! legend ("1st Bar", "2nd Bar", "3rd Bar") |
10662
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
308 |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
309 %!demo |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
310 %! clf |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
311 %! x = 0:0.1:7; |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
312 %! h = plot (x, sin(x), x, cos(x), x, sin(x.^2/10), x, cos(x.^2/10)); |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
313 %! title ("Only the sin() objects have keylabels") |
3afcd24ced61
legend.m: Key labels for specified objects.
Ben Abbott <bpabbott@mac.com>
parents:
10549
diff
changeset
|
314 %! legend (h([1, 3]), {"sin(x)", "sin(x^2/10)"}, "location", "southwest") |