Mercurial > hg > octave-nkf
annotate scripts/plot/legend.m @ 9457:f9fb8c1a8e45
legend.m: Add an hggroup demo using bar(). Include clf() in each demo.
author | Ben Abbott <bpabbott@mac.com> |
---|---|
date | Thu, 23 Jul 2009 18:04:49 -0400 |
parents | 350148cc0774 |
children | 95c3e38098bf |
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}) |
6146 | 27 ## @deftypefnx {Function File} {} legend ('@var{func}') |
28 ## | |
6895 | 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. | |
6146 | 34 ## |
6895 | 35 ## The optional parameter @var{pos} specifies the location of the legend |
36 ## as follows: | |
6146 | 37 ## |
6977 | 38 ## @multitable @columnfractions 0.06 0.14 0.80 |
39 ## @item @tab north @tab | |
40 ## center top | |
41 ## @item @tab south @tab | |
42 ## center bottom | |
43 ## @item @tab east @tab | |
44 ## right center | |
45 ## @item @tab west @tab | |
46 ## left center | |
47 ## @item @tab northeast @tab | |
48 ## right top (default) | |
49 ## @item @tab northwest @tab | |
50 ## left top | |
51 ## @item @tab southeast @tab | |
52 ## right bottom | |
53 ## @item @tab southwest @tab | |
54 ## left bottom | |
55 ## @item | |
56 ## @item @tab outside @tab | |
57 ## can be appended to any location string | |
6146 | 58 ## @end multitable |
59 ## | |
7001 | 60 ## Some specific functions are directly available using @var{func}: |
6146 | 61 ## |
7148 | 62 ## @table @asis |
6895 | 63 ## @item "show" |
6146 | 64 ## Show legends from the plot |
6895 | 65 ## @item "hide" |
7148 | 66 ## @itemx "off" |
6146 | 67 ## Hide legends from the plot |
6895 | 68 ## @item "boxon" |
6146 | 69 ## Draw a box around legends |
6895 | 70 ## @item "boxoff" |
6146 | 71 ## Withdraw the box around legends |
6895 | 72 ## @item "left" |
6146 | 73 ## Text is to the left of the keys |
6895 | 74 ## @item "right" |
6146 | 75 ## Text is to the right of the keys |
76 ## @end table | |
77 ## @end deftypefn | |
78 | |
79 function legend (varargin) | |
80 | |
8208 | 81 [ca, varargin, nargin] = __plt_get_axis_arg__ ("legend", varargin{:}); |
6257 | 82 nargs = nargin; |
6146 | 83 |
84 if (nargs > 0) | |
85 pos = varargin{nargs}; | |
86 if (isnumeric (pos) && isscalar (pos) && round (pos) == pos) | |
6395 | 87 if (pos >= -1 && pos <= 4) |
6257 | 88 set (ca, "keypos", pos); |
6146 | 89 nargs--; |
90 else | |
91 error ("legend: invalid position specified"); | |
92 endif | |
93 endif | |
94 endif | |
6977 | 95 |
96 if (nargs > 1) | |
97 pos = varargin{nargs-1}; | |
7054 | 98 str = varargin{nargs}; |
6977 | 99 if (strcmpi (pos, "location") && ischar (str)) |
100 set (ca, "keypos", str); | |
101 nargs -= 2; | |
102 endif | |
103 endif | |
6146 | 104 |
6257 | 105 kids = get (ca, "children"); |
106 nkids = numel (kids); | |
107 k = 1; | |
108 turn_on_legend = false; | |
109 | |
6146 | 110 if (nargs == 1) |
111 arg = varargin{1}; | |
112 if (ischar (arg)) | |
113 if (rows (arg) == 1) | |
114 str = tolower (deblank (arg)); | |
115 switch (str) | |
116 case {"off", "hide"} | |
6257 | 117 set (ca, "key", "off"); |
9451 | 118 nargs--; |
6146 | 119 case "show" |
6257 | 120 set (ca, "key", "on"); |
9451 | 121 nargs--; |
6146 | 122 case "toggle" |
6257 | 123 val = get (ca, "key"); |
8190
73d6b71788c0
use case-insensitive comparison for graphics properties; misc style fixes
John W. Eaton <jwe@octave.org>
parents:
8102
diff
changeset
|
124 if (strcmpi (val, "on")) |
6257 | 125 set (ca, "key", "off"); |
126 else | |
127 set (ca, "key", "on"); | |
128 endif | |
9451 | 129 nargs--; |
6146 | 130 case "boxon" |
6257 | 131 set (ca, "key", "on", "keybox", "on"); |
9451 | 132 nargs--; |
6146 | 133 case "boxoff" |
6257 | 134 set (ca, "keybox", "off"); |
9451 | 135 nargs--; |
8291
53f35799b235
Add support for left/right argument to the legend function
David Bateman <dbateman@free.fr>
parents:
8257
diff
changeset
|
136 case "left" |
53f35799b235
Add support for left/right argument to the legend function
David Bateman <dbateman@free.fr>
parents:
8257
diff
changeset
|
137 set (ca, "keyreverse", "off") |
9451 | 138 nargs--; |
8291
53f35799b235
Add support for left/right argument to the legend function
David Bateman <dbateman@free.fr>
parents:
8257
diff
changeset
|
139 case "right" |
53f35799b235
Add support for left/right argument to the legend function
David Bateman <dbateman@free.fr>
parents:
8257
diff
changeset
|
140 set (ca, "keyreverse", "on") |
9451 | 141 nargs--; |
6146 | 142 otherwise |
143 endswitch | |
144 else | |
145 varargin = cellstr (arg); | |
6740 | 146 nargs = numel (varargin); |
6146 | 147 endif |
148 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
|
149 varargin = arg; |
6146 | 150 nargs = numel (varargin); |
151 else | |
152 error ("legend: expecting argument to be a character string"); | |
153 endif | |
154 endif | |
155 | |
6272 | 156 if (nargs > 0) |
157 have_data = false; | |
9451 | 158 for k = 1:nkids |
159 typ = get (kids(k), "type"); | |
160 if (strcmp (typ, "line") || strcmp (typ, "surface") | |
161 || strcmp (typ, "patch") || strcmp (typ, "hggroup")) | |
6272 | 162 have_data = true; |
163 break; | |
164 endif | |
165 endfor | |
166 if (! have_data) | |
167 warning ("legend: plot data is empty; setting key labels has no effect"); | |
168 endif | |
6147 | 169 endif |
170 | |
6575 | 171 warned = false; |
9451 | 172 k = nkids; |
173 for i = 1:nargs | |
6146 | 174 arg = varargin{i}; |
175 if (ischar (arg)) | |
9451 | 176 typ = get (kids(k), "type"); |
177 while (k > 1 | |
178 && ! (strcmp (typ, "line") || strcmp (typ, "surface") | |
179 || strcmp (typ, "patch") || strcmp (typ, "hggroup"))) | |
180 typ = get (kids(--k), "type"); | |
6257 | 181 endwhile |
9451 | 182 if (k > 0) |
8208 | 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); | |
189 break; | |
190 endif | |
191 endfor | |
192 else | |
193 set (kids(k), "keylabel", arg); | |
194 endif | |
6257 | 195 turn_on_legend = true; |
9451 | 196 if (--k == 0) |
197 break; | |
198 endif | |
6257 | 199 elseif (! warned) |
200 warned = true; | |
201 warning ("legend: ignoring extra labels"); | |
202 endif | |
6146 | 203 else |
204 error ("legend: expecting argument to be a character string"); | |
205 endif | |
206 endfor | |
207 | |
6257 | 208 if (turn_on_legend) |
209 set (ca, "key", "on"); | |
6146 | 210 endif |
211 | |
212 endfunction | |
213 | |
214 %!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
|
215 %! clf |
8343
9f34f7636fe0
legend.m: Correct ording of legend labels.
Ben Abbott <bpabbott@mac.com>
parents:
8291
diff
changeset
|
216 %! 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
|
217 %! 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
|
218 %! 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
|
219 |
9f34f7636fe0
legend.m: Correct ording of legend labels.
Ben Abbott <bpabbott@mac.com>
parents:
8291
diff
changeset
|
220 %!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
|
221 %! clf |
9451 | 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") | |
225 | |
226 %!demo | |
227 %! clf | |
6146 | 228 %! plot(1:10, 1:10); |
229 %! title("a very long label can sometimes cause problems"); | |
6977 | 230 %! legend({"hello world"}, "location", "northeastoutside") |
6146 | 231 |
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); |
235 %! title("a very long label can sometimes cause problems"); | |
236 %! legend("hello world", "location", "northeastoutside") | |
237 | |
238 %!demo | |
239 %! clf | |
6146 | 240 %! labels = {}; |
241 %! 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
|
242 %! 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
|
243 %! 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
|
244 %! labels = {labels{:}, cstrcat("Signal ", num2str(i))}; |
6146 | 245 %! endfor; hold off; |
246 %! title("Signals with random offset and uniform noise") | |
247 %! xlabel("Sample Nr [k]"); ylabel("Amplitude [V]"); | |
6977 | 248 %! legend(labels, "location", "southoutside") |
6146 | 249 %! legend("boxon") |
9451 | 250 |
251 %!demo | |
252 %! clf | |
253 %! labels = {}; | |
254 %! for i = 1:5 | |
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))}; | |
258 %! endfor; hold off; | |
259 %! title("Signals with random offset and uniform noise") | |
260 %! xlabel("Sample Nr [k]"); ylabel("Amplitude [V]"); | |
261 %! legend(labels{:}, "location", "southoutside") | |
262 %! legend("boxon") | |
263 | |
264 %!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
|
265 %! clf |
9451 | 266 %! x = linspace (0, 10); |
267 %! plot (x, x); | |
268 %! hold ("on"); | |
269 %! stem (x, x.^2, 'g') | |
270 %! legend ("linear"); | |
271 %! hold ("off"); | |
272 | |
273 %!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
|
274 %! clf |
9451 | 275 %! x = linspace (0, 10); |
276 %! plot (x, x, x, x.^2); | |
277 %! legend ("linear"); | |
278 | |
279 %!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
|
280 %! clf |
9451 | 281 %! x = linspace (0, 10); |
282 %! plot (x, x, x, x.^2); | |
283 %! 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
|
284 |
f9fb8c1a8e45
legend.m: Add an hggroup demo using bar(). Include clf() in each demo.
Ben Abbott <bpabbott@mac.com>
parents:
9451
diff
changeset
|
285 %!demo |
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 |
f9fb8c1a8e45
legend.m: Add an hggroup demo using bar(). Include clf() in each demo.
Ben Abbott <bpabbott@mac.com>
parents:
9451
diff
changeset
|
287 %! 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
|
288 %! 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
|
289 %! 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
|
290 |
f9fb8c1a8e45
legend.m: Add an hggroup demo using bar(). Include clf() in each demo.
Ben Abbott <bpabbott@mac.com>
parents:
9451
diff
changeset
|
291 %!demo |
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 |
f9fb8c1a8e45
legend.m: Add an hggroup demo using bar(). Include clf() in each demo.
Ben Abbott <bpabbott@mac.com>
parents:
9451
diff
changeset
|
293 %! 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
|
294 %! 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
|
295 %! legend ("1st Bar", "2nd Bar", "3rd Bar") |